App 跳轉內建 Map App

2016-05-05  本文已影响148人  ShinrenPan
// 創建 coordinate
CLLocationCoordinate2D coordinate =
CLLocationCoordinate2DMake(latitude, longitude);

// 創建 MKPlacemark
MKPlacemark *placemark = [[MKPlacemark alloc]initWithCoordinate:coordinate
                                              addressDictionary:nil];

// 創建 MKMapItem
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
mapItem.name = @"Target place";

// 跳轉至 Map App
[mapItem openInMapsWithLaunchOptions:nil];

// 或是使用 Class method 替代
// [MKMapItem openMapsWithItems:@[mapItem] launchOptions:nil];
NSString *address    = @"403台中市西區健行路1049號";
CLGeocoder *geocoder = [[CLGeocoder alloc]init];

void (^handler)(NSArray *geoMarks, NSError *error) = ^(NSArray *geoMarks, NSError *error){
    CLPlacemark *geoMark    = geoMarks.firstObject;
    MKPlacemark *targetMark = [[MKPlacemark alloc]initWithPlacemark:geoMark];
    MKMapItem *targetItem   = [[MKMapItem alloc]initWithPlacemark:targetMark];
    
    [targetItem openInMapsWithLaunchOptions:nil];
};

[geocoder geocodeAddressString:address completionHandler:handler];
/** 這裡以 User 目前位置跟 Target 位置做規劃 */

// 使用者目前位置
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];

// Target 位置, 按照上兩個方式創建
 MKMapItem *targetItem = ......

// 路徑方式, 這裡以開車做規劃
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};

// 跳轉至 Map App, 只能使用 Class method
[MKMapItem openMapsWithItems:@[currentLocationMapItem, targetItem]
               launchOptions:launchOptions];
上一篇 下一篇

猜你喜欢

热点阅读