每个标记显示一个WindowInfo(谷歌地图)

时间:2013-09-26 10:55:05

标签: google-maps-api-3

我试试这个,但我只能持续windowInfo。 请帮帮我

function initialize() {
var mapOptions = {
    zoom: 5,
    center: new google.maps.LatLng(33.414837,54.68141),
    mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map'),mapOptions);
/*var weatherLayer = new google.maps.weather.WeatherLayer({
    temperatureUnits: google.maps.weather.TemperatureUnit.Degree
});
weatherLayer.setMap(map);*/
/*var cloudLayer = new google.maps.weather.CloudLayer();
cloudLayer.setMap(map)*/
for (i = 0; i < StationListArray.length; i++) {
    var image = StationListArray[i].split("|")[4];
    var StationLocation = new google.maps.LatLng(StationListArray[i].split("|")[2], StationListArray[i].split("|")[1]);
    var marker = new google.maps.Marker({
        position: StationLocation,
        map: map,
        title: StationListArray[i].split("|")[0],
        icon: image
    });
    google.maps.event.addListener(marker,'click',function() {
    var infowindow = new google.maps.InfoWindow({
    content:"Hello World!"});
    infowindow.open(map,marker); 
    });

} 
}
google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:1)

将open()的第二个参数更改为this

infowindow.open(map,this);

marker指的是variable标记,每次迭代都会被覆盖。循环完成后,它指向已创建的最后一个标记。 this内的click - 回调是指已点击的标记。