簡體   English   中英

在 Swift 中將 CLLocation 轉換為 MKMapItem

[英]Convert CLLocation to MKMapItem in Swift

我從 Core Location 的 locationManager 中檢索到了用戶位置的CLLocation 為了計算到地圖項的距離,我想將CLLocation轉換為MKMapItem

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()

這樣也省去了獲取用戶位置的麻煩! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM