在标记构造函数中设置图标后,标记图标会同时显示传统图标和自定义图标

时间:2019-09-30 16:29:32

标签: javascript html css google-maps google-maps-api-3

我在视图上有一张地图,其中显示了一些标记并带有信息框。 一切正常,但是当我将地图的icon属性设置为唯一图像时,标记会显示两个图像,而我的唯一图标会覆盖默认的google pin。 这是当前的样子: !https://imgur.com/a/6P1tgE7

我在map构造函数上尝试了许多定义,但是结果是相同的。

var image = 'http://maps.google.com/mapfiles/ms/icons/rangerstation.png';

//Add markers to view
function addMarker(x, y, ward, community, lganame, projecttype) {
    var location = new google.maps.LatLng(x, y);
    var marker = new google.maps.Marker({
        position: location,
            map: map,
            title: ward
        });

    marker.setIcon(image);

    addInfoWindowToMarker(marker, ward, community, lganame, projecttype);
}

我希望显示的图标只是我指定的图标,并且完全忽略默认的Google Maps标记。

1 个答案:

答案 0 :(得分:0)

我通过更改标记定义来解决了这个问题。 请注意,我还缩小了尺寸或更好的显示方式。

        function addMarker(x, y, ward, community, lganame, projecttype) {
            var location = new google.maps.LatLng(x, y);
            var marker = new google.maps.Marker({
                position: location,
                map: map,
                title: ward,
                //icon: 'http://maps.google.com/mapfiles/ms/icons/rangerstation.png',
                icon: new google.maps.MarkerImage(
                'http://maps.google.com/mapfiles/ms/icons/rangerstation.png',
                null, /* size is determined at runtime */
                null, /* origin is 0,0 */
                null, /* anchor is bottom center of the scaled image */
                new google.maps.Size(20, 20)
              )
            });
            //Call info windows and bounce event listener so the marker animates when it is clicked
            addInfoWindowToMarker(marker, ward, community, lganame, projecttype);
        }

It works just fine now.