Skip to content Skip to sidebar Skip to footer

Hide Markers On Click With Google Maps Api

I am building a search form using Google Maps Javascript V3 API. I would like to add some filters that will hide certain types of markers on the map when clicked. The markers to hi

Solution 1:

As commented by geocodezip this approach will not work, because i will always point behind the end of locations. Additionally this would hide at best 1 marker (the last marker that has been created).

Possible approach:

Store the visible-state of the markers in a MVCObject:

map-options:

var mapOptions = {
    center: { lat: 48.509532, lng: -122.643852}
    //that's the property where we store the state
    visibility:new google.maps.MVCObject
  };

inside the loop, after the creation of marker:

//when the state is not set yet, set itif(typeof map.get('visibility').get(locations[i][11])==='undefined'){
  map.get('visibility').set(locations[i][11],true);
}
//bind the visible-property of the marker to the state
marker.bindTo('visible',map.get('visibility'),locations[i][11]);

add the listener(outside of the loop):

 google.maps.event.addDomListener(
   document.getElementsByClassName('hideOtherMarkers')[0], 
   'click', 
    function() {
       //simply set the desired property of the MVCObject to false
       map.get('visibility').set('Other',false);
    });

Demo: http://jsfiddle.net/doktormolle/5L2392mL/

Post a Comment for "Hide Markers On Click With Google Maps Api"