2014-04-10 54 views
-1

我在谷歌地圖的多個標記以下代碼,我的問題是一旦我把我的數據在地圖上他們的信息窗口都在錯誤的座標,他們似乎混亂。我以美國爲中心。所以這是下面的代碼:谷歌地圖信息窗口錯誤的位置

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

+0

你可以提供一個小提琴? – Tzach

+0

還有一些關於標記出現在哪裏以及它們在哪裏出現的筆記。 – Andy

+1

對於我來說,使用正確的初始化函數可以。 –

回答

0

下面的語句可以 這必將幫助ü

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

爲什麼要在浮點數上使用parseInt? (座標是浮動的,將它們截斷爲整數將肯定會把它們放在錯誤的地方。 – geocodezip

+0

uhm爲什麼downvote? –