如何从坐标获取地址

时间:2018-10-12 10:15:27

标签: 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 2A主路,L-30 2A主路,   卡纳塔克邦560102,印度@ <+ 12.91597974,+ 77.62879254> +/- 100.00m,   区域CLCircularRegion(标识符:'<+ 12.91597974,+ 77.62879254>   半径70.94',中心:<+ 12.91597974,+ 77.62879254>,半径:70.94m)]

我只想要我在下面提到的地址

  

班加罗尔HSR布局的L-30 2主干道,L-30 2主干道,   卡纳塔克邦560102

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

4 个答案:

答案 0 :(得分:3)

更新

我对iVarun's解决方案做了一些修改。这比较简单。和工作。

首先,添加此功能:

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

答案 1 :(得分:2)

希望这对您有帮助:

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

输出:

  

印度卡纳塔克邦班加罗尔,高铁布局2A主路L-30,邮编560102

答案 2 :(得分:1)

雨燕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
}

答案 3 :(得分:0)

CLGeocodeCompletionHandler包含CLPlacemark的数组。您可以访问其属性,例如name, locality, isoCountryCode等以形成完整的地址!