0

我已成功为谷歌地图上的标记添加了自定义图像。问题在于,作为气球的默认标记仍然与新图像一起出现。

应该如何删除默认标记并仅使用自定义图像?以下是我使用的代码块:

    var image = "image url";   
    marker = createMarker(point, address1, town, postcode, SiteName);
    marker = new google.maps.Marker({
    position: point,
    map: map,
    icon: image
    });

createMarker 函数的代码如下:

       function createMarker(point, address1, town, postcode, sitename) {

        var html;
        var infowindow;
        html = '<b>' + sitename + '</b>' + '<br/>' + address1 + '<br/>' + town + '<br/>' + postcode;
        var marker = new google.maps.Marker({ position: point, map: map });            

        google.maps.event.addListener(marker, "mouseover", function () {
            if (infowindow) infowindow.close();
            infowindow = new google.maps.InfoWindow({ content: html });
            infowindow.open(map, marker);
        });   

        google.maps.event.addListener(marker, "click", function () {
            if (infowindow) infowindow.close();
            infowindow = new google.maps.InfoWindow({ content: html });
            infowindow.open(map, marker);
        });            
        return marker;
    }
4

1 回答 1

4

图标属性-可以设置图像或矢量路径:示例-

  marker = new google.maps.Marker({
    position: new google.maps.LatLng(-25.363882, 131.044922),
    icon: {
    path: google.maps.SymbolPath.CIRCLE, 
    },
    or
    icon: url of an image,
    draggable: true,
    map: map,
    animation: google.maps.Animation.DROP
});

此图标属性将覆盖默认气球图标

或者,如果您想稍后在代码中设置图标 - 只需编写 -marker.setIcon("your url");


更新:您实际上是在同一位置创建两个标记,一个带有自定义图标,一个带有默认图标。这里-

marker = createMarker(point, address1, town, postcode, SiteName);`// marker1`
    marker = new google.maps.Marker({ `...... //marker 2`

所以把这两者结合起来——

function createMarker(point, address1, town, postcode, sitename, image) {
//code

var marker = new google.maps.Marker({ position: point, map: map, icon:image }); 
于 2012-11-02T07:42:40.157 回答