Mapkit 通过按钮打开地图

Mapkit Open Maps via Button

我创建了一个基于地图的应用程序,它在地图上有各种注释,单击它会打开一个小信息页面,如下所示。

我想知道如何 link 'Open in Maps' 按钮获取注释的坐标并打开地图以提供从用户当前位置(已编码)到注释的路线.

感谢您的帮助

您将必须根据位置信息创建 mapItem,然后使用 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];

使用 openInMapsWithLaunchOptions(),如@TKearsley 所说。

要使用该功能,您需要 MKMapItem

如果您在文档中查找 MKMapItem,您可以使用初始化方法 init(placemark:) 创建一个 MKMapItem,它需要一个 MKPlacemark.

所以下一个问题是如何创建 MKPlacemark:

如果您再次查阅文档,您会找到 MKPlacemark:

的初始值设定项
 init(coordinate: CLLocationCoordinate2D)

所以,把它们放在一起:

获取注释的坐标。

在调用 MKPlacemark's 初始值设定项时使用坐标:

 init(coordinate: CLLocationCoordinate2D)

然后在 MKMapItem 的初始化器中使用结果 MKPlacemarkinit(placemark:)