点击标记时如何更新Google Maps标记图像

时间:2019-01-12 08:45:29

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

点击标记后,我想更改标记图标并显示信息窗口。 我的问题是如何更改标记图标?

我已经存储了Google Maps实例,并尝试通过该标记的设置iconView属性对其进行更新

func setUpMarker() {
        let markerView = UIImageView(image: UIImage(named: "pinImage"))
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lng)
        marker.iconView = markerView

        storeMarker = marker
    }

更新标记的方法

func updateMarker() {
        let markerView = UIImageView(image: UIImage(named: "circleImage"))
        storeMarker.iconView = markerView
    }

但是,这会在前一个图标上添加新图标

2 个答案:

答案 0 :(得分:1)

在注释中,您询问了如何取消选择当前选定的标记。这涉及更多。我这样做是这样的:

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    // `markers` is an array of all the markers currently on the map.
    markers.forEach {
        ($0.iconView as! UIImageView).image = #imageLiteral(resourceName: "unselected-pin")
    }
    (marker.iconView as! UIImageView).image = #imageLiteral(resourceName: "selected-pin")
    return true
}

上一个答案

func mapView(_ mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
    print("tapped on marker")
    if storeMarker == marker {
        (marker.iconView as! UIImageView).image = #imageLiteral(resourceName: "circleImage")
    }
    return true
}

答案 1 :(得分:0)

您需要实现Google地图委托方法

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {}

 func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {}

 func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {}