我正在使用谷歌地图应用程序开发一个网页,但我遇到了一些问题。就目前而言,网页有一个功能地图(没有任何图层)和一个搜索栏。我是编程新手,所以希望有一个我缺少的快速修复。
当我查找地址时,地标位于它应该在的位置。但是,当我使用不同的地址进行第二次搜索时,第一次搜索的地标仍然可见,因此屏幕上有两个地标。如何让新地标替换旧地标?
<script type="text/javascript">
var geocoder;
var map;
var marker;
function initialize() {
geocoder = new google.maps.Geocoder ();
var latlng = new google.maps.LatLng (55.1667, -114.4000);
var myOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
marker = new google.maps.Marker({map:map});
}
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);
marker.setposition(results [0].geometry.location);
map.setZoom(16);
}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>