2015-05-05 41 views
1

IOS newb刚刚学习Mapkit。我使用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]; 
} 

感谢您的任何建议。

回答

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

+0

因此,请在地图视图页面订阅该mapviewdelegate协议,b成为自己的代表,听取didselectannotate,然后在didselectannotate方法中调用openinmaps时? – user1904273

+0

将mapView委托设置为管理mapView的控制器。 self.mapView.delegate = self; 然后实施代理协议到管理控制器,特别是didSelectAnnotation:方法 – flovilmart

+0

好的。标记这个权利,但意识到我想做一些不同的事情,当用户点击触摸注释后出现的弹出窗口时,打开本地地图应用程序。 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )注释 MKAnnotationView * annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@“loc”]; annotationView.canShowCallout = YES; annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; return annotationView; } 但卡在这里。 – user1904273

相关问题