我嘗試運行這篇文章(Swift - Generate an Address Format from Reverse Geocoding)的答案中提供的代碼,以從緯度和經度資訊中獲取地址資訊。當我嘗試在 Swift Playground(App Store 中的獨立游戲和 Xcode 14 中包含的游戲)中運行它時,它在第一次運行時似乎沒有做任何事情。然而,在深入研究之后,似乎有某種不好的例外。
import CoreLocation
func getAddressFromLatLon(pdblLatitude: String, withLongitude pdblLongitude: String) {
var center : CLLocationCoordinate2D = CLLocationCoordinate2D()
let lat: Double = Double("\(pdblLatitude)")!
//21.228124
let lon: Double = Double("\(pdblLongitude)")!
//72.833770
let ceo: CLGeocoder = CLGeocoder()
center.latitude = lat
center.longitude = lon
let loc: CLLocation = CLLocation(latitude:center.latitude, longitude: center.longitude)
ceo.reverseGeocodeLocation(loc, completionHandler: { (placemarks, error) in
if (error != nil)
{
print("reverse geodcode fail: \(error!.localizedDescription)")
}
let pm = placemarks! as [CLPlacemark]
if pm.count > 0 {
let pm = placemarks![0]
print(pm.country)
print(pm.locality)
print(pm.subLocality)
print(pm.thoroughfare)
print(pm.postalCode)
print(pm.subThoroughfare)
var addressString : String = ""
if pm.subLocality != nil {
addressString = addressString pm.subLocality! ", "
}
if pm.thoroughfare != nil {
addressString = addressString pm.thoroughfare! ", "
}
if pm.locality != nil {
addressString = addressString pm.locality! ", "
}
if pm.country != nil {
addressString = addressString pm.country! ", "
}
if pm.postalCode != nil {
addressString = addressString pm.postalCode! " "
}
print(addressString)
}
})
}
getAddressFromLatLon(pdblLatitude: "42.48001070918", withLongitude: "-76.4511703657")
查看它說的錯誤:
錯誤:執行被中斷,原因:信號 SIGABRT。行程一直停留在被中斷的地方,使用“thread return -x”回傳運算式求值前的狀態。
我沒有關注正在發生的事情。如果我在 SwiftUI 專案中使用此代碼,我也會遇到停止程式的錯誤例外。這似乎是 async function 的一個問題reverseGeocodeLocation
,但我不確定為什么錯誤沒有被捕獲,即使它應該在閉包中處理它。
我也嘗試使用import MapKit
而不是import Location
. 使用 MapKit,它不會出錯,但代碼似乎什么也不做。坐標應該是紐約州蘭辛市。我在這里想念什么?
uj5u.com熱心網友回復:
在玩了一些代碼之后,我找到了錯誤的原因。它是任何嘗試創建CLLocationCoordinate2D
. 很奇怪。由于您可以輕松地CLLocation
直接從lat
和lon
值創建實體,因此可以避免錯誤。
這是您的代碼的一個經過大量清理的版本。此代碼消除了所有強制解包。以下在 Xcode 14.1 操場上運行得很好。
除了所有代碼清理之外,洗掉使用CLLocationCoordinate2D
解決了主要問題。
import CoreLocation
func getAddressFromLatLon(pdblLatitude: String, withLongitude pdblLongitude: String) {
guard let lat = Double(pdblLatitude),
let lon = Double(pdblLongitude) else { return }
let loc = CLLocation(latitude: lat, longitude: lon)
let ceo = CLGeocoder()
ceo.reverseGeocodeLocation(loc, completionHandler: { (placemarks, error) in
if let error {
print("reverse geodcode fail: \(error)")
} else if let placemarks, let pm = placemarks.first {
var addressString = ""
if let val = pm.subLocality {
addressString = val ", "
}
if let val = pm.thoroughfare {
addressString = val ", "
}
if let val = pm.locality {
addressString = val ", "
}
if let val = pm.country {
addressString = val ", "
}
if let val = pm.postalCode {
addressString = val " "
}
print(addressString)
}
})
}
getAddressFromLatLon(pdblLatitude: "42.48001070918", withLongitude: "-76.4511703657")
即使您在 Xcode 14.1 中創建一個全新的 iOS Playground 并僅輸入以下代碼,它也會使 Playground 崩潰:
import CoreLocation
print("Before")
let center = CLLocationCoordinate2D()
print("After")
輸出:
在
libc abi 之前:以 NSException 型別的未捕獲例外終止
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533056.html
標籤:迅速地图包核心位置