6

我有一个纬度/经度位置,我想将其转换为 Swift 中的位置名称字符串。做这个的最好方式是什么?我相信最好使用 reverseGeocodeLocation 功能,但不完全确定如何使用。这是我到目前为止所拥有的:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    if (locationFixAchieved == false) {
        locationFixAchieved = true
        var locationArray = locations as NSArray
        var locationObj = locationArray.lastObject as CLLocation
        var coord = locationObj.coordinate
        var latitude = coord.latitude
        var longitude = coord.longitude

        getCurrentWeatherData("\(longitude)", latitude: "\(latitude)")
        reverseGeocodeLocation(location:coord, completionHandler: { (<#[AnyObject]!#>, <#NSError!#>) -> Void in
            <#code#>
        })

    }
}

func reverseGeocodeLocation(location: CLLocation!, completionHandler: CLGeocodeCompletionHandler!){

}
4

4 回答 4

13

您需要编写如下代码:

geocoder.reverseGeocodeLocation(currentLocation, completionHandler: {
            placemarks, error in

                if error == nil && placemarks.count > 0 {
                    self.placeMark = placemarks.last as? CLPlacemark
                    self.adressLabel.text = "\(self.placeMark!.thoroughfare)\n\(self.placeMark!.postalCode) \(self.placeMark!.locality)\n\(self.placeMark!.country)"
                    self.manager.stopUpdatingLocation()
                }
            })
于 2014-10-07T08:32:21.687 回答
4

please check this answer.

 func getAddressFromGeocodeCoordinate(coordinate: CLLocationCoordinate2D) {
    let geocoder = GMSGeocoder()
    geocoder.reverseGeocodeCoordinate(coordinate) { response , error in

      //Add this line
      if let address = response!.firstResult() {
        let lines = address.lines! as [String]
        print(lines)

      }
    }
  }
于 2016-04-19T14:58:30.877 回答
4

我正在使用 Swift 3 / XCode 8

我使用了Benjamin Herzog 的答案,但遇到了一些与可选和强制转换相关的构建错误。

在重写它时,我决定将它封装在一个函数中并对其进行泛化,以便它可以轻松地插入到任何地方。

import CoreLocation

func getPlacemark(forLocation location: CLLocation, completionHandler: @escaping (CLPlacemark?, String?) -> ()) {
    let geocoder = CLGeocoder()

    geocoder.reverseGeocodeLocation(location, completionHandler: {
        placemarks, error in

        if let err = error {
            completionHandler(nil, err.localizedDescription)
        } else if let placemarkArray = placemarks {
            if let placemark = placemarkArray.first {
                completionHandler(placemark, nil)
            } else {
                completionHandler(nil, "Placemark was nil")
            }
        } else {
            completionHandler(nil, "Unknown error")
        }
    })

}

使用它:

getPlacemark(forLocation: originLocation) { 
    (originPlacemark, error) in
        if let err = error {
            print(err)
        } else if let placemark = originPlacemark {
            // Do something with the placemark
        }
    })
}
于 2016-10-04T21:05:54.283 回答
0

获取地标地址的 Swift 代码,格式很好

let address = ABCreateStringWithAddressDictionary(placemark.addressDictionary!, false).componentsSeparatedByString("\n").joinWithSeparator(", ")

print(address!)
于 2016-07-12T10:24:22.597 回答