I know it's possible to start the iPhone maps application by calling openURL
on a google maps URL with parameters saddr
and daddr
with location strings or Lat/Long (see example below).
But I'm wondering if it's possible to make the start address be the "Current Location" maps bookmark so that I can use the Maps app's location handling code. My Google search has been pretty fruitless.
For example:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%@&daddr=%@", myLatLong, latlong]]];
Except with something to invoke the current location bookmark in place of myLatLong
.
Choose a different starting point or destination: Tap My Location, tap either the From or To field, then enter a different location.
You need to use Core Location to get the current location, but with that lat/long pair, you can get Maps to route you from there, to a street address or location. Like so:
CLLocationCoordinate2D currentLocation = [self getCurrentLocation]; // this uses an address for the destination. can use lat/long, too with %f,%f format NSString* address = @"123 Main St., New York, NY, 10001"; NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@", currentLocation.latitude, currentLocation.longitude, [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
Finally, if you do want to avoid using CoreLocation to explicitly find the current location, and want to use the @"http://maps.google.com/maps?saddr=Current+Location&daddr=%@"
url instead, then see this link that I provided in comments below for how to localize the Current+Location string. However, you are taking advantage of another undocumented feature, and as Jason McCreary points out below, it may not work reliably in future releases.
Originally, Maps used Google maps, but now, Apple and Google have separate maps apps.
1) If you wish to route using the Google Maps app, use the comgooglemaps URL scheme:
NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&directionsmode=driving", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; BOOL opened = [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
2) To use Apple Maps, you can use the new MKMapItem
class for iOS 6. See the Apple API docs here
Basically, you will use something like this, if routing to destination coordinates (latlong
):
MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: latlong addressDictionary: nil]; MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place]; destination.name = @"Name Here!"; NSArray* items = [[NSArray alloc] initWithObjects: destination, nil]; NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsDirectionsModeKey, nil]; [MKMapItem openMapsWithItems: items launchOptions: options];
In order to support both iOS 6+ and pre iOS 6 in the same code, I'd recommend using something like this code that Apple has on the MKMapItem
API doc page:
Class itemClass = [MKMapItem class]; if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { // iOS 6 MKMapItem available } else { // use pre iOS 6 technique }
This would assume that your Xcode Base SDK is iOS 6 (or Latest iOS).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With