Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I change marker options without using marker = new google.maps.Marker

I have found the function below which creates only one marker - which is what I want. But how do I change the marker options e.g. html - without creating a new one?
i.e. the code below will move an existing marker using setPosition but what if I also want its html and title changed....

var marker;

function placeMarker(location) {
if ( marker ) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
  position: location,
  map: map
});
}
}
like image 704
webewitch Avatar asked Dec 30 '25 22:12

webewitch


1 Answers

The html is the content of the infoWindow bound to the marker's 'click' event. There is an infoWindow.setContent() method. I would extend the marker to hold the html content when you create it, then update it where you reset the position, title, etc. Then you need to write you own 'click' event handler to use something against a single global infoWindow.

google.maps.event.addListener(marker, 'click', function() {
                infoWindow.setContent(marker.html);
                infowindow.open(map,marker);
            });
like image 90
Eric Bridger Avatar answered Jan 04 '26 16:01

Eric Bridger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!