Question

I'm currently working on an app with a map view that opens up Apple Maps and sends in longitude and latitude so a start and stop location. So the user can navigate with the help of the Apple Maps. My question is: Is it possible so make the Apple Maps app to automatically run the navigation function? As it is now it opens the Apple Maps app with the right coordinates and annotation on the start location and the stop location, but I have to click the navigation function in order to get Apple Maps to present a navigated route from start to stop.

Here is the code that opens up Apple Maps from my own app:

// NAVIGATION ACTION SHEET

        if (buttonIndex == 0) {
            double toLatDouble = [to_lat doubleValue];
            double toLongDouble = [to_long doubleValue];
            CLLocationCoordinate2D location = CLLocationCoordinate2DMake(toLatDouble,toLongDouble);

            // Apple Maps, using the MKMapItem class
            MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:location addressDictionary:nil];
            MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark];
            item.name = self.toLocation;
            [item openInMapsWithLaunchOptions:nil];

        }
        else if (buttonIndex == 1){
            double fromLatDouble = [from_lat doubleValue];
            double fromLongDouble = [from_long doubleValue];
            CLLocationCoordinate2D location = CLLocationCoordinate2DMake(fromLatDouble,fromLongDouble);

            // Apple Maps, using the MKMapItem class
            MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:location addressDictionary:nil];
            MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark];
            item.name = self.toLocation;
            [item openInMapsWithLaunchOptions:nil];

        }else {
            // Cancel
            NSLog(@"User canceled navigation actionSheet");
        }
Was it helpful?

Solution

As stated in the documentation of MKMapItem when you call the openInMapsWithLaunchOptions: you can also pass some options. One of them activates navigation mode.

For example, to start directions for driving, you should write:

[item openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey :MKLaunchOptionsDirectionsModeDriving}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top