在 Swift 中将 CLLocation 转换为 MKMapItem
Convert CLLocation to MKMapItem in Swift
我有一个 CLLocation
用于我从 Core Location 的 locationManager 中检索到的用户位置。为了计算到地图项的距离,我想将 CLLocation
转换为 MKMapItem
。
我想你可以得到 CLLocation
的坐标,从中得到一个 MKPlacemark
,最后从 MKPlacemark
中得到一个 MKMapItem
,做如下的事情:
let currentLocation:CLLocation = locationManager.location
var coord : CLLocationCoordinate2D = currentLocation.coordinate
let myPlacemark = MKPlacemark(coordinate: coord)
let myMapItem = MKMapItem(placemark: myPlacemark)
对于这项任务来说,这似乎相当冗长,我想知道,是否没有更直接的方法来做到这一点?
提前感谢您的任何建议。
由于您要为其创建 MKMapItem 的是用户的当前位置,因此您可以这样做:
let myMapItem = MKMapItem.mapItemForCurrentLocation()
这也省去了获取用户位置的麻烦! :)
我有一个 CLLocation
用于我从 Core Location 的 locationManager 中检索到的用户位置。为了计算到地图项的距离,我想将 CLLocation
转换为 MKMapItem
。
我想你可以得到 CLLocation
的坐标,从中得到一个 MKPlacemark
,最后从 MKPlacemark
中得到一个 MKMapItem
,做如下的事情:
let currentLocation:CLLocation = locationManager.location
var coord : CLLocationCoordinate2D = currentLocation.coordinate
let myPlacemark = MKPlacemark(coordinate: coord)
let myMapItem = MKMapItem(placemark: myPlacemark)
对于这项任务来说,这似乎相当冗长,我想知道,是否没有更直接的方法来做到这一点?
提前感谢您的任何建议。
由于您要为其创建 MKMapItem 的是用户的当前位置,因此您可以这样做:
let myMapItem = MKMapItem.mapItemForCurrentLocation()
这也省去了获取用户位置的麻烦! :)