Does anyone know if it's possible to invoke the native Apple Maps application on an iOS 8 device from within a third-party app and pass in the user's Current Location
(if they give permission) as well as a destination Latitude and Longitude
to allow the Maps app to plot a route?
I have a relatively simple application which shows an MKMapView
with various locations plotted on it in a local area. Tapping on one shows an annotation, which in turn allows a modal view to be presented with more information about the premises. I have a "Get Directions" option which I want to simply capture the users location and open up the Maps app with a route calculated from their location to the premises that they've selected without the overhead of having to do it all myself in this app - no budget for handling UI, errors, routing etc (just thought it would be a nice feature if it could be done as simply as handing off to the Maps app).
Any help or advice appreciated.
Yes, it's quite possible, easy, and well documented. Take a look at the MKMapItem class, and the method openMapsWithItems:launchOptions:
method. If you set up the options correctly it will display driving directions.
Thanks to @Duncan C for pointing me in the right direction. You can actually invoke the maps app using URL schemes and pass in a few parameters but it's also possible through the points giving by Duncan. Here's the full code I ended up with to do exactly what I required.
// Check to see if we can use Maps app.
// Pass an MKMapItem (destination) in along with Current Location
// Specify what kind of directions we want (driving, Walking etc).
// Invoke the Maps app.
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
{
// An MKMapItem which we will pass to Maps app.
// I'm using properties from my Shop object.
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(_shop.coordinate.latitude, _shop.coordinate.longitude);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:[NSString stringWithFormat:@"Branch Name: %@", _shop.branch.name]];
// Set the directions mode to "Driving"
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving};
// Get the "Current User Location" MKMapItem
MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation];
// Pass the current location and destination map items to the Maps app
[MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Directions Error"
message:@"Sorry, we've encountered a problem meaning we're unable to provide you with directions at the moment.\n\nPlease use the Call Branch or Text Branch options to get in touch with a representative."
delegate:self
cancelButtonTitle:@"Dmiss"
otherButtonTitles:nil];
[alert show];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.