IOS/MapKit:点击 MKPlacemark 启动本地地图应用程序

IOS/MapKit: Launch Native Map App by Clicking on MKPlacemark

IOS 刚学Mapkit的新手。我使用 MKPlacemark 在我的应用程序中加载地图。然而,有些用户可能想要使用更高级的功能,例如行车路线,为此,我认为,他们最好在我的应用程序之上启动本机应用程序(当他们完成常规地图时,我的应用程序仍在后台打开)应用)

我知道如何使用 MKMapItem 从我的应用程序启动本机应用程序。但是,只有在用户触摸位置标记后才能启动本机应用程序。

这是我正在使用的代码。

-(void) geoCodeAndMapIt {
    NSString* location = @"156 University Ave, Palo Alto, CA 94301";
    NSLog(@"going to map this address:  %@",location);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
             completionHandler:^(NSArray* placemarks, NSError* error){
                 if (placemarks && placemarks.count > 0) {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     MKPlacemark *placemark = [[MKPlacemark alloc]
                                               initWithPlacemark:topResult];
                     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(placemark.coordinate, 5000, 5000);//5000 is meters
                     region.span.longitudeDelta /= 8.0;
                     region.span.latitudeDelta /= 8.0;

                     [self.mapView setRegion:region animated:YES];
                     [self.mapView addAnnotation:placemark];

 //                    The following MKMapItem class launches the full blown native app.  Commenting it out causes the map to load in the app.  Otherwise, it fires up the native map app immediately in place of the previous app.

                     MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:placemark];
                     mapItem.name = self.contact.first;
                     mapItem.phoneNumber = self.contact.tel;

                     NSDictionary *options = @{
                                               MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                                               MKLaunchOptionsMapTypeKey:
                                                   [NSNumber numberWithInteger:MKMapTypeSatellite],
                                               MKLaunchOptionsShowsTrafficKey:@YES
                                               };
                     [mapItem setName:@"Name of your location"];
                     [mapItem openInMapsWithLaunchOptions:options];*/


                 }
             }
 ];


    [mapItem openInMapsWithLaunchOptions:options];
}

感谢您的任何建议。

只有在 didSelectAnnotation 上调用 MKMapViewDelegate 时才应调用 openInMaps:例如。

https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapViewDelegate_Protocol/index.html#//apple_ref/occ/intf/MKMapViewDelegate

要打开地图应用,您还可以使用以下内容自行构建 URL:

UIApplication.sharedApplication().openURL(...)

在此处查看此文档以获取其余信息:

https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html