开发者

How to open Maps Application from other Application in iPhone SDK

Why when I'm opening

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=Current Location&daddr=123 Main St,Ottawa,ON"]];

Maps application isn't opening? Moreover, even Safari isn't opening.

But when I opening: http://google.com开发者_如何学Python, Safari is normally opening.


Well... As this question over 2 years old and has received over 3k views without an answer I think it's about time someone posts one. Mainly because obviously traffic keeps appearing here.


In anycase you can find the answer in various other parts of this site but the one that worked like a charm for me is this one: https://stackoverflow.com/a/12432512/525576

The great part of this answer is it address iOS 5 and below and iOS 6 and above.

I've edited the answer a bit to fit arc standards that automatically come with iOS 7.

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude,longitude);

//create MKMapItem out of coordinates
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem* destination =  [[MKMapItem alloc] initWithPlacemark:placeMark];

if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
    //using iOS6 native maps app
    [destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];        
} 
else
{
    //using iOS 5 which has the Google Maps application
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f", latitude, longitude];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜