ios - 在 Swift 中将 CLLocation 转换为 MKMapItem
问题描述
我有一个CLLocation
从核心位置的 locationManager 中检索到的用户位置。为了计算到地图项的距离,我想将 转换CLLocation
为MKMapItem
.
我想你可以得到 的坐标CLLocation
,从中制作一个MKPlacemark
,最后MKMapItem
从MKPlacemark
做如下的事情中制作一个:
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()
这也为您省去了获取用户位置的麻烦!:)
推荐阅读
- ios - 是否有任何经验法则来决定是覆盖 UIView 的绘制功能,还是在实现子视图时使用 CAShapeLayer?
- flutter - Flutter - 如何在本地保存数据?
- powershell - 递归替换win10文件夹和文件中的非标准字符
- php - 如何在 PHP 中使用依赖注入?
- reactjs - 我不明白为什么这个扬声器组件需要 React.memo 来防止额外的渲染
- python - 如何使用正则表达式找到具有可变长度前缀的重叠匹配?
- visual-studio-code - Visual Studio Code 删除自动完成
- jquery - 将方法存储在将在“if”条件下使用的变量中是不好的做法还是不正确?
- android - 菜单项可见性不会部分改变
- fat-free-framework - Fatfree 框架嵌套模板