2013-08-26 205 views
0

我想刷新所有標記。我使用了一個setInterval函數。但是,我如何刪除我的標記?這時標記被添加到舊的標記......那不是我想要的。Google地圖刷新標記

所以這是我的代碼:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
    <style type="text/css"> 
     html { height: 100% } 
     body { height: 100%; margin: 0; padding: 0 } 
     #map_canvas { height: 100% } 
    </style> 
    <script type="text/javascript" 
     src="http://maps.googleapis.com/maps/api/js?key=xyz&sensor=true"> 
    </script> 
    <script type="text/javascript"> 
     var map; 
     function initialize() { 
     var mapOptions = { 
      center: new google.maps.LatLng(48.137, 11.577), 
      zoom: 14, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(document.getElementById("map_canvas"), 
      mapOptions); 
     } 
    </script> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>  
    </head> 
    <body onload="initialize()"> 
    <div id="map_canvas" style="width:100%; height:100%"></div> 
    <script type="text/javascript"> 
setInterval(function() { 
     $(document).ready(function() { 
     $.getJSON('./url.php', function(data) { 
      var cars = data.rec.vehicles.vehicles; 
      $.each(cars, function(key, data) { 
      var LatLng = new google.maps.LatLng(data.position.latitude, data.position.longitude); 
      var imageh = data.model; 
      var bild = "/" + imageh + ".png"; 
      if(data.fuelState <=25) 
       {bild="/EMPTY.png";} 
      var bild1= "/" + imageh + "1.png"; 
      var tanken1=""; 
      if(data.fuelState<=25){tanken1="TANKEN";} 
      console.log(bild); 
      var contentString ='<h1>'+data.model+" "+data.carName+'</h1><IMG BORDER="0" ALIGN="center" SRC="' + data.model + '1.png"><br><br>Kennzeichen: '+ data.licensePlate +'<br>Fahrgestellnummer: '+data.vin+'<br>Fuellstand: ' + data.fuelState + '% <IMG BORDER="0" ALIGN="absmiddle" SRC="' + data.fuelState + '.png"><br>Zustand: <IMG BORDER="0" align="absmiddle" SRC="' + data.innerCleanliness + '.png"><br>Adresse: ' +data.address+'<br><a href="https://de.drive-now.com/php/metropolis/vehicle_details?vin='+ data.vin +'">Dieses Fahrzeug jetzt Buchen!</a><br><br>'+tanken1; 

      var infowindow = new google.maps.InfoWindow({ 
      content: contentString 
      }); 
      var marker = new google.maps.Marker({ 
       position: LatLng, 
       title: data.model, 
       icon: bild 
      }); 
      google.maps.event.addListener(marker, 'click', function() { 
      infowindow.open(map,marker); 
      google.maps.event.addListener(map, 'click', function() {if(infowindow){infowindow.close();}}); 
      }); 

      marker.setMap(map); 
      }); 
     }); 
     }); 
     clearMarkers(); 
     }, 10000); 

     $(document).ready(function() { 
     $.getJSON('./PetrolStations.txt', function(data) { 
      $.each(data, function(key, data) { 
      var LatLng = new google.maps.LatLng(data.lat, data.lng); 
      var bild1 = "/PETROL.png"; 
      var contentString1 = data.name; 
      var infowindow = new google.maps.InfoWindow({ 
      content: contentString1 
      }); 
      var marker = new google.maps.Marker({ 
       position: LatLng, 
       title: data.lng, 
       icon: bild1 
      }); 
      google.maps.event.addListener(marker, 'click', function() { 
      infowindow.open(map,marker); 
      google.maps.event.addListener(map, 'click', function() {if(infowindow){infowindow.close();}}); 
      }); 

      marker.setMap(map); 
      }); 
     }); 
     }); 
    </script> 
    </body> 
</html> 

謝謝:))

回答

1

保持引用已創建(在數組)的標記。

要刪除它們,遍歷每個標記上調用.setMap(null)的數組。

+0

在這整個事情中我是新的..你能給我一些代碼提示嗎?謝謝 :)) – user2718017