错误位置的Google地图信息窗口

时间:2014-04-10 11:20:06

标签: javascript jquery google-maps-api-3

我正在关注谷歌地图中的多个标记的代码,我的问题是,一旦我将我的数据放在地图上,他们的信息窗口都在错误的坐标中,它们似乎是乱七八糟的。我用美国作为我的中心。所以这是下面的代码:

var locations = [
    ['Russ Martin</br>Montana',43.299428,-74.217933],
    ['Carey Fischer</br>New York',46.879682,-110.362566],
    ['Brandon Born</br>Connecticut',40.714353,-74.005973],
    ['Joe Tocyloski</br>Pennysylvania',41.603221,-73.087749]
];

var us  = new google.maps.LatLng(42.746632,-75.770041);
var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: us,
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
        /*icon:{
          strokeColor:'green',
          scale:3
        },*/
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map,
        animation:google.maps.Animation.DROP

    });

    google.maps.event.addListener(marker, 'mouseover', (function(marker, i) {
        return function() {
            infowindow.setContent(locations[i][0]);
            infowindow.open(map, marker);
        }
    })(marker, i));

    google.maps.event.addListener(marker, 'mouseout', (function(marker, i) {
        return function() {
            infowindow.setContent(locations[i][0]);
            infowindow.close(map, marker);
        }
    })(marker, i));
    console.log(locations);
}

任何帮助都将不胜感激。

Fiddle

1 个答案:

答案 0 :(得分:0)

以下声明可能 这肯定会帮助你

return function() {
            infowindow.setContent(locations[i][0]);
            infowindow.open(map, this);
        }