谷歌地图iOS反向地理编码

在将UILabel的文本设置为反向地理编码地址时,我遇到了Google Maps Reverse Geocoding的问题。 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 

任何帮助肯定是赞赏!

您正在使用地理编码的大括号返回,因此最后一个代码可以正常工作。 你应该做这样的事情:

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

并调用该function

  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