【问题标题】:Google Maps iOS Reverse Geocoding谷歌地图 iOS 反向地理编码
【发布时间】:2016-09-19 06:19:21
【问题描述】:

在将 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

非常感谢任何帮助!

【问题讨论】:

    标签: ios swift google-maps reverse-geocoding


    【解决方案1】:

    您正在使用地理编码括号外的返回值,因此最后一个代码有效。你应该这样做:

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

    【讨论】:

    • 不幸的是,这也不起作用。外部函数必须获取坐标并返回地址。我修改了您提供的作为起点的内容以接受坐标,但是,当我调用 getAddress() 时,它按预期返回地址,但我仍然无法将 UILabel 文本设置为 infoWindow 中的地址。
    【解决方案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
        }
    }
    

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

    let infoWindow = Bundle.main.loadNibNamed("InfoWindowCurrent", owner: self, options: nil)?[0] as! InfoWindowCurrent
    
    infoWindow.labelAddress.text = marker.userData as? String
    
    return infoWindow
    

    【讨论】:

      【解决方案3】:

      第一个响应对我有用。作为替代

      marker.title = lines.joined(separator: "\n")`
      

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

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

      对于城市,试试

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-27
        • 2014-12-17
        • 1970-01-01
        • 2013-07-29
        • 1970-01-01
        • 2014-04-06
        • 1970-01-01
        相关资源
        最近更新 更多