【问题标题】:How to Update Google Maps Marker Image when tap on the marker点击标记时如何更新谷歌地图标记图像
【发布时间】:2019-06-07 01:16:43
【问题描述】:

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

我已经存储了谷歌地图实例,并尝试将其更新为该标记的设置 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
    }

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

【问题讨论】:

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


    【解决方案1】:

    你需要实现谷歌地图委托方法

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

    【讨论】:

    • 我已经添加了那些委托方法,你能告诉我我应该在这些方法中做什么。
    【解决方案2】:

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

    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
    }
    

    【讨论】:

    • 哇,这很简单。你知道如何在点击另一个标记时取消选择“选定”标记吗?
    猜你喜欢
    • 2012-09-27
    • 2015-02-16
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 2012-02-21
    • 2012-12-04
    相关资源
    最近更新 更多