IOS / MapKit:点击MKPlacemark启动Native Map App

时间:2015-05-05 23:49:22

标签: ios mapkit mkmapitem mkplacemark

IOS newb刚学习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];
}

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

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

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

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

UIApplication.sharedApplication()。的OpenURL(...)

此处查看此文档以了解其余内容:

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