18
18

More than 5 years have passed since last update.

自作アプリから標準のマップアプリに経路案内をさせる

Posted at

自作の地図系アプリで,始点と終点を指定して経路案内をさせてみました.

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];

思ったより簡単にマップを開けます.自分自身を経路制御アプリケーションにするのは,ちょっと敷居が高いので今度挑戦してみます.

18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18