首页 > 解决方案 > 在 Swift 中将 CLLocation 转换为 MKMapItem

问题描述

我有一个CLLocation从核心位置的 locationManager 中检索到的用户位置。为了计算到地图项的距离,我想将 转换CLLocationMKMapItem.

我想你可以得到 的坐标CLLocation,从中制作一个MKPlacemark,最后MKMapItemMKPlacemark做如下的事情中制作一个:

let currentLocation:CLLocation = locationManager.location
var coord : CLLocationCoordinate2D = currentLocation.coordinate
let myPlacemark = MKPlacemark(coordinate: coord)
let myMapItem = MKMapItem(placemark: myPlacemark)

对于这项任务来说,这似乎相当冗长,我想知道是否没有更直接的方法可以做到这一点?

在此先感谢您的任何建议。

标签: iosswiftmapkitcore-location

解决方案


由于它是您尝试创建 MKMapItem 的用户当前位置,因此您可以这样做:

let myMapItem = MKMapItem.mapItemForCurrentLocation()

这也为您省去了获取用户位置的麻烦!:)


推荐阅读