Google Maps API v.3:如果未在网址找到,请使用默认标记图标

时间:2014-07-31 12:04:30

标签: google-maps icons google-maps-markers

目前谷歌地图api没有显示任何图标,如果标记的图标属性设置为以404返回的网址。如果用户提供的图标网址被破坏,我如何告诉谷歌地图使用默认的砖色标记图标?我找不到一种方法来确定图标是否实际找到或返回404。

示例:

var marker = new google.maps.Marker({
    position: point,
    map: googlemap,
    title: "Click here!",
    icon: SOME_URL_THAT_FAILS (or not)
});

1 个答案:

答案 0 :(得分:2)

我找到了以下解决自己案例的方法。如果有人需要这个:

var icon = {
    url: SOME_URL_THAT_FAILS (or not)
};

var marker = new google.maps.Marker({
    position: point,
    map: googlemap,
    title: "Some Title"
});

var i = new Image();
i.src = SOME_URL_THAT_FAILS (or not);

i.onload = function () {
    marker.setIcon(icon); //If icon found go ahead and show it
}
i.onerror = function () {
    marker.setIcon(null); //This displays brick colored standard marker icon in case image is not found.
}