代码之家  ›  专栏  ›  技术社区  ›  Gowthaman M manas.abrol

如何从坐标中得到地址

  •  1
  • Gowthaman M manas.abrol  · 技术社区  · 6 年前

    我想从坐标上得到地址。我在下面附上了我的代码。。

    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干道,L-30 2nd A干道,高铁布局,班加罗尔, 卡纳塔克邦560102,印度@<+12.91597974,+77.62879254>+/-100.00米, 半径70.94',中心:<+12.91597974,+77.62879254>,radius:70.94m)]

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

    L-30 2nd A干道,L-30 2nd A干道,高铁布局,班加罗尔,

    4 回复  |  直到 6 年前
        1
  •  5
  •   excitedmicrobe    6 年前

    更新

    我已经对它做了一些修改 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

        2
  •  2
  •   iVarun    6 年前

    希望这能帮助您:

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

    输出:

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

        3
  •  1
  •   Vinaykrishnan    6 年前

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

    函数正在将placemark分析为字符串。。。

    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
    }
    
        4
  •  0
  •   iOSer    6 年前

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

    推荐文章