2013-08-27 77 views
1

我的位置查找器顯示多個標記,但我不想那樣。 我對谷歌API並不擅長,所以我希望你能幫助我。在谷歌地圖上顯示多個標記

人們需要填寫他們的地址,然後他需要顯示位置。沒有顯示多個標記。 (只有一個)

這裏是JS:

 var geocoder; 
     var map; 

     function initialize() { 
     geocoder = new google.maps.Geocoder(); 
     var latlng = new google.maps.LatLng(52.132633, 5.291266); 
     var myOptions = { 
      zoom: 7, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(document.getElementById("map_canvas"), 
      myOptions); 
     } 

     function codeAddress() { 
     var sAddress = document.getElementById("inputTextAddress").value; 
     document.getElementById("map_canvas").style.removeProperty('display'); 
     geocoder.geocode({ 'address': sAddress}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 

       map.setCenter(results[0].geometry.location); 
       map.setZoom(10); 

       var marker = new google.maps.Marker({ 
        map: map, 
        position: results[0].geometry.location 
       }); 
      } else { 
       alert("Helaas, er is iets fout gegaan. Foutcode: " + status); 
      } 
      }); 
     } 

回答

0

如果您希望您的標記的控制權,你需要將它們添加到一個數組,然後添加一個新的之前刪除所有的人:

markersArray = [] 
    function codeAddress() { 
    var sAddress = document.getElementById("inputTextAddress").value; 
    document.getElementById("map_canvas").style.removeProperty('display'); 
    geocoder.geocode({ 'address': sAddress}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 

      map.setCenter(results[0].geometry.location); 
      map.setZoom(10); 

      var marker = new google.maps.Marker({ 
       map: map, 
       position: results[0].geometry.location 
      }); 
      RemoveMarkers(); 
      markersArray.push(marker) 
     } else { 
      alert("Helaas, er is iets fout gegaan. Foutcode: " + status); 
     } 
     }); 
    } 


    function RemoveMarkers(){ 
     for(i=0;i<markersArray.length;i++){ 
      markersArray[i].remove() // I don't remember exactly the name of the gmaps method  
     } 
    } 

我還沒有測試代碼,但這是你需要的想法!

+0

謝謝@jbartolome爲您的答案!它仍然不起作用。看到這裏的例子:http://bvweijen.nl/23g/web/nogps.php – Bvweijen

+1

它的工作!我用.setMap(null)來清除標記! – Bvweijen

+0

太棒了!我很高興它有幫助! :) – jbartolome