每次我的循环使用指定的setInterval运行时,我在更新infowindow内容时遇到问题。每1秒infowindow的内容将更改每个标记数组...但是我遇到问题如果我把闭包放在if中声明。在5分钟之后,浏览器会因为每1秒钟添加一次监听器而崩溃。我想要的只是更新每个标记的内容。我会这样做吗?
提前谢谢。
var map;
var marker;
var markerarray =[];
setInterval(function(){
$.ajax({
type: "post",
url: "vehiclecordinates.php",
success: function(data){
coordinates = data.latlng;
vehiclename = data.vehiclename;
heading = data.heading;
devname = data.vehiclename;
thedate = data.trackdate;
for (var i = 0; i < coordinates.length; i++) {
newcoordinate = new google.maps.LatLng(coordinates[i].split(",")[0],coordinates[i].split(",")[1]);
if (markerarray[vehiclename[i]] && markerarray[vehiclename[i]].setPosition){
markerarray[vehiclename[i]].icon.rotation = parseInt(heading[i]);
markerarray[vehiclename[i]].setPosition(newcoordinate);
var con = '<div style="font: 11px arial,tahoma,helvetica,sans-serif;">Vehicle Name:' + devname + '<br/>' +'Date='+thedate+'</div>';
}else {
marker = new MarkerWithLabel({
map:map,
labelClass: "mylabels",
labelStyle: {opacity: 1.0},
labelContent: '<div>'+ vehiclename[i]+'</div>',
icon:{
path: google.maps.SymbolPath.CIRCLE,
scale:.7,
strokeColor: 'white',
strokeWeight: .10,
fillOpacity: 1,
fillColor: '#404040',
offset: '5%',
rotation: parseInt(heading[i]),
anchor: new google.maps.Point(10, 50)
}
});
marker.setPosition(newcoordinate);
markerarray[vehiclename[i]] = marker;
var con = '<div style="font: 11px arial,tahoma,helvetica,sans-serif;">Vehicle Name:' + devname + '<br/>' +'Date='+thedate+'</div>';
google.maps.event.addListener( marker, 'click', (function (marker, con, infowindow) {
return function () {
infowindow.setContent(con);
infowindow.open(map, marker);
};
})( marker, con, infowindow));
}
}
}
});
},1000);
答案 0 :(得分:0)
您需要为匿名clickEventListener
功能命名。
然后使用javacript code to remove click event。
如果您使用jquery添加eventlistener,那么您可以轻松地unbind()
该函数,或者搜索它是否存在然后不添加。
您可以设置一个bool
:clickSet=true
,在调用eventlistener之后设置。
编辑:
代码:
function whatever(marker, con, infowindow) {
return function () {
infowindow.setContent(con);
infowindow.open(map, marker);
};
};
var map;
var marker;
var markerarray =[];
setInterval(function(){
$.ajax({
type: "post",
url: "vehiclecordinates.php",
success: function(data){
coordinates = data.latlng;
vehiclename = data.vehiclename;
heading = data.heading;
devname = data.vehiclename;
thedate = data.trackdate;
for (var i = 0; i < coordinates.length; i++) {
newcoordinate = new google.maps.LatLng(coordinates[i].split(",")[0],coordinates[i].split(",")[1]);
if (markerarray[vehiclename[i]] && markerarray[vehiclename[i]].setPosition){
markerarray[vehiclename[i]].icon.rotation = parseInt(heading[i]);
markerarray[vehiclename[i]].setPosition(newcoordinate);
var con = '<div style="font: 11px arial,tahoma,helvetica,sans-serif;">Vehicle Name:' + devname + '<br/>' +'Date='+thedate+'</div>';
}else {
marker = new MarkerWithLabel({
map:map,
labelClass: "mylabels",
labelStyle: {opacity: 1.0},
labelContent: '<div>'+ vehiclename[i]+'</div>',
icon:{
path: google.maps.SymbolPath.CIRCLE,
scale:.7,
strokeColor: 'white',
strokeWeight: .10,
fillOpacity: 1,
fillColor: '#404040',
offset: '5%',
rotation: parseInt(heading[i]),
anchor: new google.maps.Point(10, 50)
}
});
marker.setPosition(newcoordinate);
markerarray[vehiclename[i]] = marker;
var con = '<div style="font: 11px arial,tahoma,helvetica,sans-serif;">Vehicle Name:' + devname + '<br/>' +'Date='+thedate+'</div>';
google.maps.event.addListener( marker, 'click', whatever);
}
}
}
});
},1000);