1

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.  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.

user1904273
  • 4,562
  • 11
  • 45
  • 96

1 Answers1

1

You should call the openInMaps only when the MKMapViewDelegate is called on didSelectAnnotation: for ex.

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

To open the Maps app you could also build the URL yourself with the following:

UIApplication.sharedApplication().openURL(...)

Check this documentation here for the rest:

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

flovilmart
  • 1,759
  • 1
  • 11
  • 18
  • So make the map view page subscribe to that mapviewdelegate protocol, become a delegate of itself, listen for didselectannotate and then when in the didselectannotate method call openinmaps? – user1904273 May 09 '15 at 13:32
  • Set the mapView delegate to the controller managing the mapView. self.mapView.delegate = self; then implement the delegate protocol into the managing controller, particularly the didSelectAnnotation: method – flovilmart May 09 '15 at 14:28
  • Ok. Marking this right but realize I want to do something different, open the native map app when user clicks on the popup that appears after touching the annotation. Something like - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation { MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"]; annotationView.canShowCallout = YES; annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; return annotationView; } But stuck here. – user1904273 May 10 '15 at 15:41
  • SO use that one: mapView(_:annotationView:calloutAccessoryControlTapped:) – flovilmart May 10 '15 at 16:47