0

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, Safari is normally opening.

citizen conn
  • 15,300
  • 3
  • 58
  • 80
Alexander Zakatnov
  • 1,646
  • 1
  • 15
  • 16
  • 1
    use canOpenURL: on UIApplication to make sure that your application is reporting that it can handle the URL you're giving it. If it reports no, then your URL is more than likely malformed in some way. Perhaps you need to percent escape it? – Paul Tiarks Jul 06 '11 at 17:37
  • possible duplicate of [How can I launch the Google Maps iPhone application from within my own native application?](http://stackoverflow.com/questions/30058/how-can-i-launch-the-google-maps-iphone-application-from-within-my-own-native-app) – Abizern Jul 25 '11 at 22:14
  • This is a duplicate of http://stackoverflow.com/questions/30058/how-can-i-launch-the-google-maps-iphone-application-from-within-my-own-native-app – Andrew Jul 06 '11 at 17:35

1 Answers1

1

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]];
}
Community
  • 1
  • 1
John Riselvato
  • 12,854
  • 5
  • 62
  • 89