GMSMarker infoWindow未更新

时间:2018-10-10 07:39:37

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

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

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中更新值。

1 个答案:

答案 0 :(得分:0)

尝试此代码

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

    var infoView:UIView!
    let markerTitle = marker.title

    for i in 0..<self.latitude.count {
        if markerTitle == self.ord_id[i] {

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

            let orderIDLbl = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 20))
            orderIDLbl.text = "Order ID: \(self.ord_id[i])"
            orderIDLbl.font = UIFont.boldSystemFont(ofSize: 17)
            orderIDLbl.textAlignment = NSTextAlignment.center
            infoView.addSubview(orderIDLbl)

            let orderIDLblBottom = orderIDLbl.frame.maxY
            let height = heightForView(text: "Prj Name: \(self.project_name[i])", font: UIFont.systemFont(ofSize: 17), width: 300)
            let proNameLbl = UILabel(frame: CGRect(x: 0, y: orderIDLblBottom, width: 300, height: height))
            proNameLbl.text = "Prj Name: \(self.project_name[i])"
            proNameLbl.font = UIFont.systemFont(ofSize: 17)
            proNameLbl.numberOfLines = 0
            proNameLbl.lineBreakMode = .byWordWrapping
            infoView.addSubview(proNameLbl)

        }
    }

    return infoView
}