我正在使用Shreerang Patwardhan的Javascript地理编码代码。但是,在输入新地址时,我对删除前一个标记感到困惑。我用谷歌搜索和实验,但我无法得到我想要的结果。我希望在放置新标记时删除/隐藏上一个标记。
HTML:
<html>
<head>
<title>Google Maps API v3 Example : Geocoding Simple</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
</script>
</head>
<body onload="initialize()">
<div align="center" style="height: 30px; width: 430px">
<input id="address" type="textbox">
<input type="button" value="Geocode" onclick="codeAddress()">
</div>
<div id="map" style="height:200px; width: 430px"></div>
</body>
</html>
使用Javascript:
var geocoder;
var map;
function initialize()
{
geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById("map"),
{
zoom: 8,
center: new google.maps.LatLng(22.7964,79.5410),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
}
function codeAddress()
{
var address = document.getElementById("address").value;
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);
}
});
}
我使用的代码与this JS Fiddle中的代码相同:
答案 0 :(得分:1)
你需要做一个marker.setMap(null);标记是对先前标记的引用
if(marker)
marker.setMap(null)