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

标签 ios swift cllocation cllocationcoordinate2d

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

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/

相关文章:

ios - 无法将类型 '() -> Void' 的值分配给类型 '(() -> Void)!'

ios - 使用嵌套多边形数组中的实际面积查找最里面的多边形

ios - 检查 CLLocation 是否处于形状中?

ios - 如何获得毫秒级精度的nsdate?

ios - UITableView 和 UICollectionView 上的 Swift didSelectRowAt 不起作用

iOS - MapKit 相机旋转动画

iOS : alert user for specific Geo location area (lat, 长)

iphone - 如何实现可以在背景上滚动的 UITableView

ios - 除了 UITextField 之外,如何为 UIImageView 提供约束?

ios - 自定义表格单元格中的 stackview