Google Maps iOS反向地理编码

rec*_*rks 5 google-maps reverse-geocoding ios swift

在将UILabel的文本设置为反向地理编码的地址时,Google Maps反向地理编码存在问题。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
Run Code Online (Sandbox Code Playgroud)

我也尝试过这个:

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

任何帮助都非常感谢!

Rob*_*Rob 5

您正在使用地理编码大括号中的返回键,因此,最后一个代码有效。您应该执行以下操作:

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)
        }
    }     
}
Run Code Online (Sandbox Code Playgroud)

并调用该函数

  getAddress() { (returnAddress) in      

    print("\(returnAddress)")

    }
Run Code Online (Sandbox Code Playgroud)


rec*_*rks 2

所以,我最终走了另一条路。它不漂亮,但很有效。每个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
    }
}
Run Code Online (Sandbox Code Playgroud)

当选择标记时,标签将设置为“userData”属性中传递的地址:

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

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

return infoWindow
Run Code Online (Sandbox Code Playgroud)