简体   繁体   English

在 Swift 中将 CLLocation 转换为 MKMapItem

[英]Convert CLLocation to MKMapItem in Swift

I have a CLLocation for the User's location that I have retrieved from Core Location's locationManager.我从 Core Location 的 locationManager 中检索到了用户位置的CLLocation In order to calculate the distance to a map item, I would like to convert the CLLocation into a MKMapItem .为了计算到地图项的距离,我想将CLLocation转换为MKMapItem

I gather you can get the coordinates of the CLLocation , make an MKPlacemark from them and finally make an MKMapItem from the MKPlacemark doing something like the following: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)

This seems rather lengthy for this task and I am wondering, if there is no more direct way to do this?对于这项任务来说,这似乎相当冗长,我想知道,是否没有更直接的方法来做到这一点?

Thanks in advance for any suggestions.在此先感谢您的任何建议。

Since it's the user's current location you're trying to create an MKMapItem of, you can just do this:由于它是您尝试为其创建 MKMapItem 的用户当前位置,因此您可以执行以下操作:

let myMapItem = MKMapItem.mapItemForCurrentLocation()

That saves you the trouble of obtaining user's location, too!这样也省去了获取用户位置的麻烦! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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