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
});
}
}

      

+3


source to share


3 answers


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);
            });

      

+3


source


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');

      

+1


source


Maker is MVCObject and this class has a set method

marker.set(property, New_Value);

      

If you want to change more than one property, you can use the method setOptions

+1


source







All Articles