2015-05-05 78 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

相關問題