Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Maps for directions from inside your app - iOS5 iOS6

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 in iOS6 is called and the directions from saddr to daddr is traced but when I am in iOS5 the maps app is called but just one pin is put at the daddr. For some unknown reason the initial coordinates (saddr) are not showing and no direction is traced.

Here is my code:

addr = [NSString stringWithFormat: @"maps://saddr=%f,%f&daddr=%f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude, oldLatitude, oldLongitude];
NSURL *url = [NSURL URLWithString:addr];
[[UIApplication sharedApplication] openURL:url];

I have tried change the URL to "http://maps.google.com/something" but it calls Safari instead of built in Maps app. I have noticed that the variables are being passed properly to the URL.

Any ideas?

Thanks in advance!

like image 908
lsp Avatar asked Sep 14 '12 03:09

lsp


People also ask

How do I change the direction on the map on iPhone?

Touch and hold anywhere on the map. Tap the search field, begin typing, then tap a result. Tap the directions button on the place card. After you tap the directions button, you can choose a different mode of travel, a different starting point, and other options. See Select other route options in Maps on iPhone.

How do I get Siri to give me directions on iOS?

See the iOS and iPadOS Feature Availability website. Say something like “Hey Siri, give me driving directions home.” Learn how to ask Siri. Tap your destination (such as a search result in Maps or a landmark on a map), or touch and hold anywhere on the map, then tap the directions button.

How do I select other route options in maps on iPhone?

Tap the directions button on the place card. After you tap the directions button, you can choose a different mode of travel, a different starting point, and other options. See Select other route options in Maps on iPhone.

How do I get travel directions in the Maps app?

You can get travel directions in the Maps app in several ways. Important: To get directions, iPhone must be connected to the internet, and Precise Location must be turned on. Cellular data rates may apply (see View or change cellular data settings on iPhone ).


1 Answers

I had a similar problem and I had to create some conditional OS code to deal with the fact that the Google Maps application has been removed. From the new MKMapItem Reference

//first create latitude longitude object
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]];
}

[placeMark release];
[destination release];

To get walking directions:

  1. For iOS 6 maps - You can set MKLaunchOptionsDirectionsModeWalking instead of MKLaunchOptionsDirectionsModeDriving
  2. For Google maps - Add &dirflg=w to the url.

I think it's better to use the openInMapsWithLaunchOptions in iOS6 because it gives you complete control over how the maps application will respond.

like image 170
dirkoneill Avatar answered Oct 13 '22 11:10

dirkoneill