我正在编写一个程序,其中包含地址,州或邮政编码。查询我们的数据库并将结果推送到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”,那么一切都很好,并且很棒。但是如果同一用户想搜索其他东西 - 比方说加利福尼亚州的一个特定的邮政编码 - 它会进行搜索,但之前的所有标记仍然存在,这使得他们的第二次搜索毫无意义。
您使用的是什么版本的API? – 2014-10-01 20:17:50
[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