Google Maps iOS Reverse Geocoding

后端 未结 3 1837
耶瑟儿~
耶瑟儿~ 2021-02-09 18:40

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 info

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-09 19:00

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

    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
    

提交回复
热议问题