用不同的地方更改谷歌地图标记图标

时间:2013-07-17 18:28:12

标签: javascript html5 google-maps

我有两个位置。我想用外部图像更改位置的标记图标。但在当前的代码中,两个地方都采用相同的图像。有什么帮助吗?

    var markers = [
     ['Sydney', lat,long],
     ['New York', lat,long],


    function initializeMaps() {
    var myOptions = {
    zoom: 3,
    center: new google.maps.LatLng(lat, long),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
};
var image = 'img/logos.png';
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, i;
for (i = 0; i < markers.length; i++) {  


    marker = new google.maps.Marker({
        position: new google.maps.LatLng(markers[i][1], markers[i][2]),
        map: map,
    icon: image
    });
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(markers[i][0]);
            infowindow.open(map, marker);
        }
    })(marker, i));
}

}

1 个答案:

答案 0 :(得分:1)

您只定义了1张图片,

为每个loc添加图像到数组,然后使用markers [i] [3]访问它,如果你把它放在最后。

这应该有效:

var markers = [
     ['Sydney', lat,long, 'url-to-image'],
     ['New York', lat,long, 'url-to-image'],


    function initializeMaps() {
    var myOptions = {
    zoom: 3,
    center: new google.maps.LatLng(lat, long),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
};
//var image = 'img/logos.png';
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
var infowindow = new google.maps.InfoWindow(), marker, i;
for (i = 0; i < markers.length; i++) {  


    marker = new google.maps.Marker({
        position: new google.maps.LatLng(markers[i][1], markers[i][2]),
        map: map,
    icon: markers[i][3]
    });
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(markers[i][0]);
            infowindow.open(map, marker);
        }
    })(marker, i));
}
}