Google地图iOS反向地理编码 [英] Google Maps iOS Reverse Geocoding

查看:535
本文介绍了Google地图iOS反向地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将Google地图反向地理编码设置为反向地理编码地址的UILabel文本时,我遇到了一个问题。 UILabel在XIB中用作自定义infowindow。对于正在正常工作的其他数据,我有另一个自定义infowindow,但似乎当我尝试在反向地理编码回调/完成处理程序中设置标签的文本时,它不起作用。这是我迄今为止的代码,我已经尝试过多种方式,包括将它分配给变量,并且我还没有能够得到任何工作。

I am having an issue with Google Maps Reverse Geocoding when it comes to setting a UILabel's text to the reverse geocoded address. The UILabel is in an XIB use as a custom infowindow. I have another custom infowindow for other data that is working correctly however, it appears that when I try to set the label's text within the reverse geocode callback / completion handler, it doesn't work. Here is the code I have so far and I have tried the code multiple ways, including assigning it to variables and I have not been able to get anything to work.

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

所有内容都正确连接,因为以下代码可以正常工作:

Everything is connected correctly because the following code works:

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

任何帮助绝对值得赞赏!

Any help is definitely appreciated!

推荐答案

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

So, I ended up going another route. It's not pretty, but it works. Each GMSMarker has a "userData" attribute that can be used to pass data. What I did was moved the marker creation into the reverse geocode completion handler and assigned the address to the "userData" attribute. Then, when the user taps to show the current address, the reverse geocode is kicked off, the marker is created and placed on the map.

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属性传递的地址:

And when the marker is selected, the label is set to the address as passed in the "userData" attribute:

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

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

return infoWindow

这篇关于Google地图iOS反向地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆