自作の地図系アプリで,始点と終点を指定して経路案内をさせてみました.
foo.h
CLLocationCoordinate2D toCoordinate;
CLLocationCoordinate2D fromCoordinate;
それぞれ,始点と終点を格納しています.今回はアプリの都合でヘッダに記述していますが,局所変数でfoo.mの関数内に書いても問題ありません.
foo.m
// 出発地のPlacemark作成
MKPlacemark *fromPlacemark = [[MKPlacemark alloc]initWithCoordinate:fromCoordinate addressDictionary:nil];
// 目的地のPlacemark作成
MKPlacemark *toPlacemark = [[MKPlacemark alloc]initWithCoordinate:toCoordinate addressDictionary:nil];
// PlacemarkからMKMapItemを作成
MKMapItem *fromMapItem = [[MKMapItem alloc]initWithPlacemark:fromPlacemark];
MKMapItem *toMapItem = [[MKMapItem alloc]initWithPlacemark:toPlacemark];
// 作成したMKMapItemをNSArrayに格納
// こうしないとマップアプリに二地点の情報を渡せません
NSArray *aryMapItems = [[NSArray alloc]initWithObjects:fromMapItem, toMapItem, nil];
// オプションの指定
// 今回は徒歩で標準マップを指定します
NSDictionary *mapOptionDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:
MKLaunchOptionsDirectionsModeWalking, MKLaunchOptionsDirectionsModeKey,
MKMapTypeStandard,MKLaunchOptionsMapTypeKey
, nil];
// この行が実行されるとマップアプリに値を渡して起動します.
[MKMapItem openMapsWithItems:aryMapItems launchOptions:mapOptionDictionary];
思ったより簡単にマップを開けます.自分自身を経路制御アプリケーションにするのは,ちょっと敷居が高いので今度挑戦してみます.