gpt4 book ai didi

ios - 如何从坐标中获取地址

转载 作者:搜寻专家 更新时间:2023-11-01 05:54:53 25 4
gpt4 key购买 nike

我想从坐标中获取地址。我在下面附上了我的代码..

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
let latvalue = lastLocation.coordinate.latitude
let lngvalue = lastLocation.coordinate.longitude
self.db_latvalue = latvalue
self.db_lngvalue = lngvalue
let location = CLLocation(latitude: latvalue, longitude:lngvalue)
let address = CLGeocoder.init()
address.reverseGeocodeLocation(CLLocation.init(latitude: latvalue, longitude:lngvalue)) { (places, error) in
if error == nil{
if let place = places{
print("addressshowingssq \(place)")
self.db_address = "\(place)"

}
}
}

输出:

[L-30 2nd A Main Road, L-30 2nd A Main Road, HSR Layout, Bengaluru, Karnataka 560102, India @ <+12.91597974,+77.62879254> +/- 100.00m, region CLCircularRegion (identifier:'<+12.91597974,+77.62879254> radius 70.94', center:<+12.91597974,+77.62879254>, radius:70.94m)]

我只想要下面提到的地址

L-30 2nd A Main Road, L-30 2nd A Main Road, HSR Layout, Bengaluru, Karnataka 560102

我研究了谷歌,我得到了不同的解决方案,所以我很困惑。

最佳答案

更新

我对iVarun 的 解决方案做了一些修改。这更简单。和工作。

首先,添加这个函数:

func geocode(latitude: Double, longitude: Double, completion: @escaping (CLPlacemark?, Error?) -> ())  {
CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude)) { completion($0?.first, $1) }
}

在那之后, 获取地址:

geocode(latitude: latvalue, longitude: lngvalue) { placemark, error in
guard let placemark = placemark, error == nil else { return }
// you should always update your UI in the main thread
DispatchQueue.main.async {
// update UI here
print("address1:", placemark.thoroughfare ?? "")
print("address2:", placemark.subThoroughfare ?? "")
print("city:", placemark.locality ?? "")
print("state:", placemark.administrativeArea ?? "")
print("zip code:", placemark.postalCode ?? "")
print("country:", placemark.country ?? "")
}
}

结果:

address1: Rua Casuarina
address2: 443
city: Rio de Janeiro
state: RJ
zip code: 20975
country: Brazil

正如@iOSer 指出的那样,CLPlacemark 能够为您提供这部分字符串,但是。

你可以拆分字符串:

let output:String = "[L-30 2nd A Main Road, L-30 2nd A Main Road, HSR Layout, Bengaluru, Karnataka 560102, India @ <+12.91597974,+77.62879254> +/- 100.00m, region CLCircularRegion (identifier:'<+12.91597974,+77.62879254> radius 70.94', center:<+12.91597974,+77.62879254>, radius:70.94m)]"
let items = output.components(separatedBy: "@")
print(items[0])

因为 @始终包含在内,您可以跳过其余部分。

结果: R

关于ios - 如何从坐标中获取地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52777349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com