Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a Google Map Marker is currently selected?

I have a simple enough map on Google Maps V3.

I change the icon image on mouse over listener event, I change it back on mouse out simple enough.

I change the icon again when I click the marker, but, I want to keep that icon while the marker is selected. When I mouse out, the marker icon changes again because I told it to do so in the mouse out listener event.

I need to exclude the selected marker from the mouseout listener event but I can't figure out how to find the marker I have currently selected. Any ideas?

Here is my code

        google.maps.event.addListener(marker, 'mouseover', function () {
            this.setIcon("images/star-3-white.png");

        });
        google.maps.event.addListener(marker, 'mouseout', function () {
                //  this overwrites the image again, 
                //  need to exclude the current one here
                this.setIcon("images/star-3.png");
        });

        google.maps.event.addListener(marker, 'click', function () {
            this.setIcon("images/star-3-white.png");
            infowindow.setContent(this.html);
            infowindow.open(map, this);
        });
like image 916
jezorama Avatar asked Dec 26 '22 19:12

jezorama


1 Answers

Either

  1. create a member of the marker .selected and set that when you click on it, then test it in the mouseout function (and the mouseover function if you want to be complete), don't change the icon if it is set.
  2. create a global variable (assuming there is only one marker selected at a time), set that equal to the marker that was clicked. In the mouseout (and mouseover) check if it is equal to the current marker (this), if it is don't change the icon.
like image 198
geocodezip Avatar answered Jun 25 '23 02:06

geocodezip