简体   繁体   中英

Mapkit Open Maps via Button

I have created a map-based app that has various annotations on a map that when clicked on open up a small info page as shown below.

显示页面

I am wondering how I can link the 'Open in Maps' button to take the coordinates of the annotation and open maps to provide a route from the users current location (already coded in) to the Annotation.

Thank you for your help

you will have to create mapItem from location information and then use open in maps ( https://developer.apple.com/reference/mapkit/mkmapitem/1452239-openinmapswithlaunchoptions?language=objc )

CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(37.7749,-122.4194);

MKPlacemark *placeMark = [[MKPlacemark alloc] initWithCoordinate:coord addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark];

NSMutableDictionary *launchOptions = [[NSMutableDictionary alloc] init];
[launchOptions setObject:MKLaunchOptionsDirectionsModeDriving forKey:MKLaunchOptionsMapTypeKey];
[mapItem openInMapsWithLaunchOptions:launchOptions];

Use openInMapsWithLaunchOptions() , as @TKearsley says.

To use that function, you need an MKMapItem .

If you look up MKMapItem in the docs, you can create an MKMapItem with the init method init(placemark:) , which expects an MKPlacemark .

So the next question is how do you create an MKPlacemark :

If you again consult the docs you'll find an initializer for MKPlacemark :

 init(coordinate: CLLocationCoordinate2D)

So, bringing it all together:

Get the coordinate of your annotation.

Use the coordinate in a call to MKPlacemark's initializer:

 init(coordinate: CLLocationCoordinate2D)

Then use the resulting MKPlacemark In MKMapItem's initializer: init(placemark:)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM