8

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!

SpaceBeers
  • 13,617
  • 6
  • 47
  • 61
lsp
  • 171
  • 1
  • 5
  • 10
  • Virtually identical question [to this older one](http://stackoverflow.com/q/576768/119114), except that the start address is the current location in the other question (doesn't really change the problem much). – Nate Apr 08 '13 at 22:13

2 Answers2

35

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.

Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
YoungDinosaur
  • 1,550
  • 17
  • 22
  • 1
    Thanks for the help mate! I follow your advice and used openInMapsWithLaunchOptions and it worked perfectly. The only trick I used was passing the current location as a parameter inside NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", currentLatitude, currentLongitude, latitude, longitude]; because I couldn't get Current+Location sintax to work. Maybe it has something to do about sandboxing in iOS 5.1.1. – lsp Sep 18 '12 at 01:36
  • @lsp, The issues with using `Current+Location` are discussed in my answer [to this very similar older question](http://stackoverflow.com/a/964131/119114), and the comments responding to it. – Nate Apr 08 '13 at 22:14
  • i dont want the map view to cover the full screen, it should display only half of my screen is that possible. – Arun Jun 24 '13 at 07:15
0

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 <AddressBook/AddressBook.h> somewhere in your code to make use of the kABPersonAddressStreetKey constant.

Nick Forge
  • 21,344
  • 7
  • 55
  • 78