代码之家  ›  专栏  ›  技术社区  ›  Let's_Create

点击标记时如何更新谷歌地图标记图像

  •  0
  • Let's_Create  · 技术社区  · 7 年前

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

    我已存储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 回复  |  直到 7 年前
        1
  •  2
  •   Daniel T.    6 年前

    在评论中,您询问了如何取消选择当前选定的标记。这有点复杂。我是这样做的:

    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
    }
    
        2
  •  0
  •   Milan    7 年前

    您需要实现Google map委托方法

    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {}
    
     func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {}
    
     func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {}