LoginSignup
17
17

More than 5 years have passed since last update.

iOS 6 でマップアプリの経路を開く

Posted at
- (void)openMapsWithLatitude:(double)latitude longitude:(double)longitude
{
    // openMapsWithItems が定義されていれば実行
    Class itemClass = [MKMapItem class];
    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
        CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

        // 緯度経度から逆変換して位置情報取得
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:[placemarks objectAtIndex:0]];

            MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
            NSArray *mapItems = [NSArray arrayWithObjects:mapItem, nil];
            // 歩いた場合の経路を表示するオプション
            NSDictionary *launchOptions = [NSDictionary dictionaryWithObjectsAndKeys:MKLaunchOptionsDirectionsModeWalking, MKLaunchOptionsDirectionsModeKey, nil];
            [MKMapItem openMapsWithItems:mapItems launchOptions:launchOptions];
        }];
    }
}

こんな感じで。

17
17
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
17
17