问题描述
var locations=[['Name0',0.0,0.0,0],['Name1',1.0,1.0,0],['Name2',2.0,2.0,0],['Name3',3.0,3.0,0],['Name41',4.0,4.0,1],['Name55',5.0,5.0,0],['Name6',6.0,6.0,1],['Name7',7.0,7.0,0],['Name8',8.0,8.0,1],['Name9',9.0,9.0,1] ]; for (var i = 0; i < locations.length; i++) { var beach = locations[i]; var myLatLng = new google.maps.LatLng(beach[1], beach[2]); //alert(beach[1]+","+beach[2]); var marker = new google.maps.Marker({ position: myLatLng, map: map, icon: (beach[3]==0?image:image2), title:beach[0] });google.maps.event.addListener(marker, 'click', function() {alert(marker.getPosition());//点击任意一个overlay结果都是(9,9)。为何?? :twisted: }); }
解决方案
主要是使用了 js 闭包。导致的。改成这样试试google.maps.event.addListener(marker, 'click', function() {var m = arguments[0];return function() {alert(m.getPosition());}}.call(marker, marker));