samedi 29 novembre 2014

Add and Remove Search Marker and Show Info Window on Google Maps API


I am building a page with Google Maps JavaScript V3 API. I have this function that searches an address and then adds a marker:



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
});
marker.setIcon('http://maps.google.com/mapfiles/arrow.png');
content = "test";
infowindow.setContent(content);
infowindow.open(map, marker);
} else {
alert('Your search was not successful for the following reason: ' + status);
}
});
}


Currently, when you search once it works as it should but when I search again I would like to remove the first marker that is placed and only show the marker for the current search address. Also how can I make it show an info window for the current search address?





Aucun commentaire:

Enregistrer un commentaire