【问题标题】:GMSMarker infoWindow not updatingGMSMarker infoWindow 未更新
【发布时间】:2019-03-15 01:24:26
【问题描述】:

我正在使用谷歌地图,它是标记。为了在标记上显示信息,我使用自定义视图来显示它。但是一旦我初始化它,值就不会更新。
下面是我的代码。

func mapView(_ mapView: GMSMapView!, markerInfoWindow marker: GMSMarker) -> UIView? {
        let location = CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude
        var markerView : MarkerInfoView = Bundle.main.loadNibNamed("MarkerInfoView", owner: self, options: nil)![0] as! MarkerInfoView
        let geoCoder = CLGeocoder()
        geoCoder.reverseGeocodeLocation(location) { (placemarkers, error) in
            if let placemarker = placemarkers?.last {
                var strAddress = ""
                if let str = placemarker.name {
                    strAddress += str
                }
                if let str = placemarker.subAdministrativeArea {
                    strAddress += ", " + str
                }
                print(strAddress)
                markerView.deviceInfo.text = "HELLO TESTING"
            print(markerView.deviceInfo.text!) // This is printing "HELLO TESTING", but not updating on marker
                markerView.addressInfo.text = strAddress
            }
        }
        if let str = marker.snippet {
            markerView.deviceInfo.text = str.components(separatedBy: "|")[0]
            //TODO: add time
            markerView.dateInfo.text = str.components(separatedBy: "|")[1]
//            markerView.addressInfo.text = ""
        }
        else {
            markerView.deviceInfo.text = ""
            markerView.dateInfo.text = ""
//            markerView.addressInfo.text = ""
        }
        return markerView

    }

请指导我如何更新 infoWindow 中的值。

【问题讨论】:

  • 你叫mapView.delegate = self
  • 是的,我已经设置了委托@iOS
  • 其实 marker.title = "这可以打印文本",但你使用的是 "markerView.deviceInfo.text" 我认为这是自定义标签。您创建此标签的位置
  • 我有 customView,因为我正在显示那些信息,如 deviceName、dateInfo 和 addressInfo。已经添加了我如何在“var markerView : MarkerInfoView ....”行中创建自定义视图的代码
  • 我,也使用自定义视图来显示内容,但是我使用了 func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? { } 功能。在这个函数中,我创建了我的标签,因此每次都会创建标签并且可以显示内容。我没有在全球范围内创建这个标签。如果你愿意,我可以发布我的代码

标签: ios swift google-maps uiviewcontroller google-maps-markers


【解决方案1】:

试试这个代码

// MARKER - GoogleMaps delegate
func mapView(_ mapView: GMSMapView, markerInfoContents marker: GMSMarker) -> UIView? {
    print("title \(markerTitle)")

    var infoView:UIView!

    infoView = UIView()
    infoView.frame = CGRect(x: 0, y: 0, width: 300, height: 75)
    infoView.backgroundColor = .black

    let orderIDLbl = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 75))
    orderIDLbl.text = "markerTitle" //Your title here
    orderIDLbl.font = UIFont.systemFont(ofSize: 15)
    orderIDLbl.textAlignment = NSTextAlignment.center
    orderIDLbl.numberOfLines = 0
    orderIDLbl.textColor = .white
    infoView.addSubview(orderIDLbl)

    return infoView
}

【讨论】:

  • 如果你想 for 循环使用它,否则删除它。并根据您的要求创建标签。
  • 我的代码进行异步请求,因此视图将在响应到达之前返回,并且当响应到达时视图不会更新这些值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多