How to change marker options without using marker = new google.maps.Marker
I found a function below which only one marker is created - this is what I want. But how to change marker parameters, eg. html - without creating a new one?
i.e. the code below moves the existing marker using setPosition, but what if I also want its html and title to be changed ....
var marker;
function placeMarker(location) {
if ( marker ) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
position: location,
map: map
});
}
}
source to share
html is the infoWindow content associated with the 'click' marker event. There is a method infoWindow.setContent (). I would extend the marker to preserve the html content when it is created and then update it where you reset the position, title, etc. Then you need to write your own 'click' event handler to use anything against a single global info window.
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(marker.html);
infowindow.open(map,marker);
});
source to share
marker object properties basically have corresponding get and set methods as detailed in the documentation
For example, Title has a get_Title () method and a set_Title () method, which you can use like this:
myMarker.setTitle('my new title');
source to share