Call Maps for directions from inside your app - iOS5 iOS6

前端 未结 2 618
鱼传尺愫
鱼传尺愫 2021-01-30 15:24

Here is a strange problem: My app should be able to call the built in Maps in iOS (both 5.1 and 6). Turns out that it works just fine under iOS6 but not under iOS5.1. The maps i

2条回答
  •  被撕碎了的回忆
    2021-01-30 16:02

    You can use MKPlacemark and MKMapItem to launch the Maps app with both a coordinate and a title on the map pin:

    NSString *pinTitle;
    CLLocationCoordinate2D coordinate;
    
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:@{(id)kABPersonAddressStreetKey: pinTitle}];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    
    if ([mapItem respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
    {
        [mapItem openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving}];
    }
    else
    {
        // Google Maps fallback
        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=Current+Location", locationItem.coordinate.latitude, locationItem.coordinate.longitude];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
    

    Note that you'll need to link against AddressBook.framework and also add #import somewhere in your code to make use of the kABPersonAddressStreetKey constant.

提交回复
热议问题