2014-10-01 43 views
1

我正在編寫一個程序,其中包含地址,州或郵政編碼。查詢我們的數據庫並將結果推送到Google Maps API的地圖和標記。如何使用Google Maps API動態刪除標記?

這裏是一個標記添加到頁面的功能:

function codeAddress(address) { 

    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 

    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
    }); 
} 

這裏是我如何推動地址的功能:可根據

function get_matches() 
     { 
      var info_ = document.getElementById('address').value; 
    $.ajax({ url: 'find_store.php', 
     data: {info: info_}, 
     type: 'get', 
     success: function(output) { 
         var html = output; 
         $('#dress').html(html); 
           var num =$('#row_c').html(); 
           var counter = 0; 
           while(counter < num) 
           { 
            var addr = $('.addre#'+ counter).html(); 
            codeAddress(addr); 

            counter++; 
           } 
             } 
}); 

} 
使用此功能

他們基本上輸入它將返回正確的商店輸出他們到一個div和無序列表。我將行數傳遞給隱藏div的「#row_c」的內部HTML,然後運行一個循環,該循環抓取輸出到屏幕的每個div的所有地址。

下面是我如何獲取用戶輸入狀態縮寫的信息的示例。

else if($type == 2) 
     { 
      $row_count = 0; 
      $z_query = "select * from store_table where state like '%$accept%' and address like '%$accept%' limit 10"; 
      $result = mysql_query($z_query); 
      $num_rows = mysql_num_rows($result); 
      if($num_rows == 0) 
      { 
      echo"<script>alert(\"Your State look-up was unsuccessful, please try again\")</script>"; 

      } 
      $width = ($num_rows * 300)."px"; 
      if($num_rows > 0) 
      { 
       echo"<div id = 'state_container' style = \" margin:none; height:200px; backround-color:hsl(0,0%,95%); position:relative; padding:0px; overflow-x:hidden; overflow-x:scroll; overflow-y:none;\">"; 
       echo"<ul style = 'list-style-type:none; margin-top:0px; margin-left:0px; padding-left:0px; width:$width;'>"; 
       while($row = mysql_fetch_assoc($result)) 
       { 
        $state = "".$row['state'].""; 
        $store = "".$row['store_name'].""; 
        $phone = "".$row['phone'].""; 
        $addr = "".$row['address'].""; 
        $website = "".$row['website'].""; 
        $state = preg_replace('/\s+/', '', $state); 
        $phone = preg_replace('/\s+/', '', $phone); 
        $website = preg_replace('/\s+/', '', $website); 
        $state = stripslashes($state); 
        $store = stripslashes($store); 
        $phone = stripslashes($phone); 
        $addr = stripslashes($addr); 
        $website = stripslashes($website); 




        echo"<li style = 'float:left; margin:none; margin-left:5px; margin-right:10px;'>"; 
        echo"<br/><b>$store</b>"; 
        echo"<br />$phone"; 
        echo"<br /><div class = 'addre' id = '$row_count' style = 'margin:0px; padding:0px;'>$addr</div>"; 
        echo"<br /><a href='$website' style = 'color:#000000; text-decoration:none;'>$website</a>"; 
        echo"</li>"; 



       $row_count++; 
       } 
       echo"<script>$('#row_c').html($row_count)</script>"; 
       echo"</ul>"; 
       echo"</div>"; 
      } 

     } 

如何刪除以前的標記?例如,如果用戶去搜索「CA」,那麼一切都很好,並且很棒。但是如果同一用戶想搜索其他東西 - 比方說加利福尼亞州的一個特定的郵政編碼 - 它會進行搜索,但之前的所有標記仍然存在,這使得他們的第二次搜索毫無意義。

+0

您使用的是什麼版本的API? – 2014-10-01 20:17:50

+0

[Google Maps API v3:如何刪除所有標記?]的可能重複(http://stackoverflow.com/questions/1544739/google-maps-api-v3-how-to-remove-all-markers) – MrUpsidown 2014-10-01 21:48:49

回答

1

首先,您需要將所有標記存儲在數組中。

var hotSpotMapMarkers = []; 

然後,當您創建標記時,將它們存儲在數組中,同時將它們分配給地圖。

hotSpotMapMarkers.push(<your marker object>); 

然後,下次刷新地圖時,請先刪除所有標記。這樣的事情:

// Clear all markers currently on the map 
for (var i = 0; i < hotSpotMapMarkers.length; i++) 
    hotSpotMapMarkers[i].setMap(null); 

此代碼是從我寫的一個應用程序完全是這樣的。順便說一句,有很多示例,可以通過使用Google找到,它顯示瞭如何執行此操作。

+0

我知道我搜索谷歌,沒有一個是太有道理的。謝謝回覆。我會試試這個。 – 2014-10-01 20:36:36

0

for (var i = 0, marker; marker = markers[i]; i++) { marker.setMap(null); }

相關問題