在GMaps API v3中更改图像的默认标记

时间:2011-05-19 21:51:33

标签: google-maps

我正在尝试调整代码(带有侧边栏的Google地图窗口),我在http://koti.mbnet.fi/ojalesa/boundsbox/makemarker_sidebar.htm找到个人博客。

我想要的是更改图像的默认标记(始终相同)。 我所有重新编码的尝试都失败了。图像位于文件夹图像中(images / monument.gif);我目前没有绝对的URL。

我尝试按照API文档在Google上搜索,但没有运气。

var infoWindow = new google.maps.InfoWindow();
var markerBounds = new google.maps.LatLngBounds();
var markerArray = [];

function makeMarker(options){
var pushPin = new google.maps.Marker({map:map});
pushPin.setOptions(options);
google.maps.event.addListener(pushPin, "click", function(){
infoWindow.setOptions(options);
infoWindow.open(map, pushPin);
if(this.sidebarButton)this.sidebarButton.button.focus();
});
var idleIcon = pushPin.getIcon();
if(options.sidebarItem){
pushPin.sidebarButton = new SidebarItem(pushPin, options);
pushPin.sidebarButton.addIn("sidebar");
}
markerBounds.extend(options.position);
markerArray.push(pushPin);
return pushPin;
}

google.maps.event.addListener(map, "click", function(){
infoWindow.close();
});

2 个答案:

答案 0 :(得分:1)

这应该有效:

var infoWindow = new google.maps.InfoWindow();
var markerBounds = new google.maps.LatLngBounds();
var markerArray = [];

function makeMarker(options) {

    var pushPin = new google.maps.Marker({
        icon: 'images/monument.gif',
        map: map
    });

    pushPin.setOptions(options);

    google.maps.event.addListener(pushPin, "click", function() {
        infoWindow.setOptions(options);
        infoWindow.open(map, pushPin);
        if (this.sidebarButton) this.sidebarButton.button.focus();
    });

    if (options.sidebarItem) {
        pushPin.sidebarButton = new SidebarItem(pushPin, options);
        pushPin.sidebarButton.addIn("sidebar");
    }

    markerBounds.extend(options.position);
    markerArray.push(pushPin);
    return pushPin;
}

google.maps.event.addListener(map, "click", function() {
    infoWindow.close();
});

唯一的变化是MarkerOptions中的“icon”属性,并删除了getIcon()调用。

答案 1 :(得分:0)

当您致电makeMarker()时,请在选项属性列表中添加icon:"images/monument.gif",如下所示:

myPushPin = makeMarker({icon:"images/monument.gif", anotherOption:"anotherOptionValue"});