如何更改 google 地图上的图标,而图层是由 kml 文件导入的?

How to change the icon on google map, while the layer is import by kml file?

我已经尝试解决这个问题一个星期了,但还没有找到任何解决方案。

我发现的都是自己放的marker是可以改的

我的情况是我导入了一个带有一些标记的 KML 文件,我想在单击事件时更改图标。 下面的代码工作正常

    var ctaLayer = new google.maps.KmlLayer({
            //KMZ KML
            url: url,
            map: map,
            suppressInfoWindows: true,
         });
    ctaLayer.addListener('click', function (kmlEvent) {     
    var text = kmlEvent.featureData.description;  
    showInContentWindow(text); }

My Current Result

大多数人使用下面的功能来设置不同的图标

marker.seticon(ICON)

但在我的情况下,我无法获得我点击的标记

希望有人能帮助我!!

使用 KmlLayer,您无法在加载后更改样式(使用 API,如当前文档所述)。一种选择是使用第三方 KML 解析器(如 geoxml3 或 geoxml_v3),它将 KML 转换为本地 google 地图 api 对象,您可以更改这些对象。

geoxml3: https://github.com/geocodezip/geoxml3

geoxml_v3: http://code.google.com/p/geoxml-v3/

根据 geocodezip 的建议

我想出了这个解决方案

        var marker;
        google.maps.event.addListener(ctaLayer, 'click', function (event) {

            var eLatLng = event.latLng;     

             if (marker != null) //this will remove the previous marker
            {
                marker.setMap(null);
            }
            marker = new google.maps.Marker({
                position: eLatLng,
                map: map,

            });

            marker.setPosition((eLatLng));
            marker.setIcon(ICON);
        });

单击 kmlLayer 标记时,我在其上覆盖了一个更大的新标记。

我知道这不是最好的解决方案,但至少它告诉用户他们现在正在单击哪个标记。