2015-04-17 97 views
0

我有问题每次更新infowindow内容我的循环运行指定的setInterval.Every 1秒的infowindow的内容将更改每个标记数组......但我我有问题如果我把关闭在if语句。 5分钟后浏览器会因为每隔1秒添加一个监听器而崩溃。我想要的只是更新每个标记的内容,我会这么做。如何更新infowindow的内容与循环setInterval

预先感谢您。

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

您需要为您的anonymus clickEventListener函数命名。

  1. 然后使用javacript code to remove click event

  2. 如果您使用jquery添加eventlistener,那么您可以很容易地unbind()该函数,或搜索它是否存在,然后不添加。

  3. 您可以设置一个boolclickSet=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); 
+0

如何把名匿名clickeventlistener? – jemz

+0

在'var map;'inicialize之前。 '函数somename(arugments){stuffToDo(); }'。然后把它称为'.addListener(marker,'click',somename)' – androbin

+0

,你能告诉我how.I不好在js – jemz