我的位置查找器显示多个标记,但我不希望这样。 我对谷歌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 :(得分: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
}
}
我没有测试过代码,但这是你需要的想法!