如何从坐标中获取地址

How to get the address from the coordinate

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

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

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

CLGeocodeCompletionHandler 包含 CLPlacemark 的数组。你可以访问它的name, locality, isoCountryCode等属性来组成一个完整的地址!!

更新

我对 iVarun's 解决方案做了一些修改。这更简单。正在工作。

首先,添加这个功能:

func geocode(latitude: Double, longitude: Double, completion: @escaping (CLPlacemark?, Error?) -> ())  {
    CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude)) { completion([=10=]?.first, ) }
}

之后, 获取地址:

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

因为 @ 总是 包括在内,您可以跳过其余部分。

结果:

Swift 3

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    let objLocation = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
    CLGeocoder().reverseGeocodeLocation(objLocation) { (placemarksArray, error) in
        if error != nil {
            print("Reverse geocoder failed with error" + (error?.localizedDescription)!)
            return
        }
        if (placemarksArray?.count)! > 0 {
            let objPlacemark = placemarksArray?[0]
            self.generateAddress(objPlacemark: objPlacemark!)
            self.locationManager?.stopUpdatingLocation()
            self.locationManager = nil
        }
        else {
            print("Problem with the data received from geocoder")
        }
    }
}

函数正在将地标解析为字符串...

func generateAddress(objPlacemark : CLPlacemark) -> String {

    print("objPlacemark : \(objPlacemark.description)")
    var completeAddress = ""

    if objPlacemark.name != nil {
        completeAddress = String(describing: objPlacemark.name!)
    }

    if objPlacemark.thoroughfare != nil && (objPlacemark.name != objPlacemark.thoroughfare) {
        completeAddress = completeAddress + ", " + String(describing: objPlacemark.thoroughfare!)
    }

    if objPlacemark.subThoroughfare != nil {
        completeAddress = completeAddress + ", " + String(describing: objPlacemark.subThoroughfare!)
    }

    if objPlacemark.subLocality != nil {
        completeAddress = completeAddress + "," + String(describing: objPlacemark.subLocality!)
    }

    if objPlacemark.locality != nil {
        completeAddress = String(describing: objPlacemark.locality!)
    }

    if objPlacemark.postalCode != nil {
        completeAddress = completeAddress + "," + String(describing: objPlacemark.postalCode!)
    }

    if objPlacemark.administrativeArea != nil {
        completeAddress = completeAddress + "," +  String(describing: objPlacemark.administrativeArea!)
    }

    if objPlacemark.isoCountryCode != nil {
        completeAddress = completeAddress + "," + String(describing: objPlacemark.isoCountryCode!)
    }

    print("completeAddress : \(completeAddress)")
    return completeAddress
}

希望对您有所帮助:

address.reverseGeocodeLocation(CLLocation.init(latitude: latvalue, longitude:lngvalue)) { (places, error) in
            if error == nil{
                let placeMark = places! as [CLPlacemark]

                if placeMark.count > 0 {
                    let placeMark = places![0]
                    var addressString : String = ""

                    if placeMark.subThoroughfare != nil {
                        addressString = addressString + placeMark.subThoroughfare! + ", "
                    }
                    if placeMark.thoroughfare != nil {
                        addressString = addressString + placeMark.thoroughfare! + ", "
                    }
                    if placeMark.subLocality != nil {
                        addressString = addressString + placeMark.subLocality! + ", "
                    }

                    if placeMark.locality != nil {
                        addressString = addressString + placeMark.locality! + ", "
                    }
                    if placeMark.administrativeArea != nil {
                        addressString = addressString + placeMark.administrativeArea! + ", "
                    }
                    if placeMark.country != nil {
                        addressString = addressString + placeMark.country! + ", "
                    }
                    if placeMark.postalCode != nil {
                        addressString = addressString + placeMark.postalCode! + " "
                    }

                    print(addressString)
                }
            }
        }

输出:

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