Nov 052012

It is often useful to open an MKMapKit annotation in the native iOS Maps app. Here’s a function that will do this for you, and takes into account the recent changes with iOS 6.

// This function accepts an MKAnnotation and opens it in the native iOS Maps App
+(void)openInMapsWithAnnotation:(id<MKAnnotation>)annotation {
    Class itemClass = [MKMapItem class];

    if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
        // ios6
        MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:annotation.coordinate addressDictionary:nil];
        MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
        [mapItem setName:[annotation title]];
        [MKMapItem openMapsWithItems:[NSArray arrayWithObject:mapItem] launchOptions:nil];        
    } else {
        // pre ios6
        NSString *base = @"maps:q=";
        CLLocationCoordinate2D location = annotation.coordinate;
        NSString *title = [[annotation title] stringByReplacingOccurrencesOfString:@" " withString:@"+"];
        NSString *urlString = [NSString stringWithFormat:@"%@%@@%f,%f", base, title, location.latitude, location.longitude];
        NSURL *url = [NSURL URLWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];

You can also find the Open MKAnnotation on GitHub code as a gist on GitHub, which you’re welcome to fork or do whatever else you want.

 Leave a Reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>