1

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

感谢您的任何建议。

4

1 回答 1

1

仅当在 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

于 2015-05-06T00:18:55.563 回答