Google 地图 iOS 反向地理编码

Google Maps iOS Reverse Geocoding

在将 UILabel 的文本设置为反向地理编码地址时,我遇到了 Google 地图反向地理编码的问题。 UILabel 在 XIB 中用作自定义信息窗口。我有另一个正常工作的其他数据的自定义信息窗口,但是,当我尝试在反向地理编码回调/完成处理程序中设置标签文本时,它似乎不起作用。这是我到目前为止的代码,我已经尝试了多种方法,包括将它分配给变量,但我无法让任何东西工作。

let infoWindow = NSBundle.mainBundle().loadNibNamed("InfoWindowCurrent", owner: self, options: nil)[0] as! InfoWindowCurrent
let geocoder = GMSGeocoder()
let coordinate = CLLocationCoordinate2DMake(Double(self.locLatitude)!, Double(self.locLongitude)!)

var currentAddress = String()

geocoder.reverseGeocodeCoordinate(coordinate) { response , error in
    if let address = response?.firstResult() {
        let lines = address.lines! as [String]

        currentAddress = lines.joinWithSeparator("\n")
    }
}

infoWindow.labelAddressStreet.text = currentAddress

return infoWindow

我也试过这个:

let infoWindow = NSBundle.mainBundle().loadNibNamed("InfoWindowCurrent", owner: self, options: nil)[0] as! InfoWindowCurrent
let geocoder = GMSGeocoder()
let coordinate = CLLocationCoordinate2DMake(Double(self.locLatitude)!, Double(self.locLongitude)!)

geocoder.reverseGeocodeCoordinate(coordinate) { response , error in
    if let address = response?.firstResult() {
        let lines = address.lines! as [String]

        infoWindow.labelAddressStreet.text = lines.joinWithSeparator("\n")
    }
}

return infoWindow

一切都已正确连接,因为以下代码有效:

let infoWindow = NSBundle.mainBundle().loadNibNamed("InfoWindowCurrent", owner: self, options: nil)[0] as! InfoWindowCurrent
let geocoder = GMSGeocoder()
let coordinate = CLLocationCoordinate2DMake(Double(self.locLatitude)!, Double(self.locLongitude)!)

geocoder.reverseGeocodeCoordinate(coordinate) { response , error in
    if let address = response?.firstResult() {
        let lines = address.lines! as [String]
    }
}

infoWindow.labelAddressStreet.text = "Unable to reverse geocode location!"

return infoWindow

非常感谢任何帮助!

您在地理编码的大括号中使用了 return,因此最后的代码有效。你应该这样做:

func getAddress(currentAdd : ( returnAddress :String)->Void){

    let infoWindow = NSBundle.mainBundle().loadNibNamed("InfoWindowCurrent", owner: self, options: nil)[0] as! InfoWindowCurrent
    let geocoder = GMSGeocoder()
    let coordinate = CLLocationCoordinate2DMake(Double(self.locLatitude)!,Double(self.locLongitude)!)


    var currentAddress = String()

    geocoder.reverseGeocodeCoordinate(coordinate) { response , error in
        if let address = response?.firstResult() {
            let lines = address.lines! as [String]

            currentAddress = lines.joinWithSeparator("\n")

            currentAdd(returnAddress: currentAddress)
        }
    }     
}

并调用该函数

  getAddress() { (returnAddress) in      

    print("\(returnAddress)")

    }

所以,我最终走了另一条路。它不漂亮,但它有效。每个 GMSMarker 都有一个 "userData" 属性,可用于传递数据。我所做的是将标记创建移动到反向地理编码完成处理程序中,并将地址分配给 "userData" 属性。然后,当用户点击以显示当前地址时,将启动反向地理编码,创建标记并将其放置在地图上。

geocoder.reverseGeocodeCoordinate(position) { response, error in
    if let location = response?.firstResult() {
        let marker = GMSMarker(position: position)
        let lines = location.lines! as [String]

        marker.userData = lines.joined(separator: "\n")
        marker.title = lines.joined(separator: "\n")
        marker.infoWindowAnchor = CGPoint(x: 0.5, y: -0.25)
        marker.accessibilityLabel = "current"
        marker.map = self.mapView

        self.mapView.animate(toLocation: position)
        self.mapView.selectedMarker = marker
    }
}

并且当标记被选中时,标签被设置为在 "userData" 属性中传递的地址:

let infoWindow = Bundle.main.loadNibNamed("InfoWindowCurrent", owner: self, options: nil)?[0] as! InfoWindowCurrent

infoWindow.labelAddress.text = marker.userData as? String

return infoWindow

第一反应对我有用。作为

的替代品
marker.title = lines.joined(separator: "\n")`

试试这个,它会给你街道地址:

marker.title = response?.firstResult()?.thoroughfare
marker.snippet = response?.firstResult()?.locality

对于城市,请尝试

marker.snippet = response?.firstResult()?.locality