2013-02-12 68 views
0

如何按时间间隔移除旧标记并自动将其替换为新标记。我正在使用谷歌地图创建一个真正的跟踪器。 `加载新标记并删除旧标记。 [复制]

var markersArray = []; 

function initialize() { 

var myLatlng = new google.maps.LatLng(39, -86); 
var map = new google.maps.Map(document.getElementById('googleMap'), { 
    zoom: 1, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}); 

$(function() { 
    $(document).ready(function(){ 
     setInterval(function(){ 
       $.ajax({          
       url: 'real.php',     //the script to call to get data   
       //data: "",      
       dataType: 'json',    //data format  
       success: function(data){   //on recieve of reply       


var locations = data; 

var infowindow = new google.maps.InfoWindow(); 
var marker, i; 
deleteOverlays(); 
for (i = 0; i < locations.length; i++) { 

    marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
    map: map 
    }); 
markersArray.push(marker); 


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


       } 
      }); 
     }, 1000); 

    }); 
}); 
} 
initialize(); 

如何删除已使用clearOverlays的标记,但地图中未显示标记。谢谢。

+0

可能重复[当使用setinterval谷歌地图v3调用函数时重复标记(http://stackoverflow.com/questions/13054782/duplicated-markers-when-calling-a-function-using-setinterval-google-maps-v3) – geocodezip 2013-02-12 15:19:01

回答

1

要删除标记,你需要设置其映射为null:

for (var ii = 0; ii < markers.length; ii++) { 
    markers[ii].setMap(null); 
} 

,或者根据您的情况,你可以只更新标记的位置:

marker.setPosition(newLatLng);