mardi 5 mai 2015

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

IOS newb just learning Mapkit. I load a map in my app using MKPlacemark. However some users may want to use more advanced features such as driving directions and for this, I think, they would be better off launching the native app on top of mine (with my app still open in the background when they finish with the regular map app)

I know how to launch the native app from my app using MKMapItem. However, s there a way to launch the native app only after the user touches the place mark.

Here is code I am using.

-(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 as opposed to the one in Idaru.  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];
}

Thanks for any suggestions.

Aucun commentaire:

Enregistrer un commentaire