Question

I'm trying to show/hide( or simply put - filter ) my markers( of houses and condos ) on my Google Map, based on what types of features user selected from #features select box.

E.g. If user selectes 'Swimming Pool' feature and clicks Submit button - show only those markers( houses/condos ) that have this feature, and hide the ones that don't have swimming pool.

Unfortunately, nothing happens/changes on my map when I run my code.

JSON of houses and condos( stored inside markers variable ):

({
condos:
    [
        {Address:'123 Fake St.', Lat:'56.645654', Lng:'23.534546', features:['Swimming Pool','BBQ','..etc..']},
        {... another condo with features ...},
        {... another condo with features ...},
        ...
    ],
houses:
    [
        {Address:'1 Main Ave.', Lat:'34.765766', Lng:'27.8786674', features:['...','...']},
        {... another house with features ...},
        {... another house with features ...},
        ...
    ]
})

JS/jQuery code:

$('#filterform').on('submit', function(e) {
    e.preventDefault(); // prevent page from reloading when form is submitted

    $('#map').gmap('set', 'bounds', null); // reset map's bounds
    $('#map').gmap('closeInfoWindow'); // close opened infoWindows

    // store selected features from the 'features' select box into features variable as array
    var features = $("#features").val();

    // for each element of type house/inside houses array
    $(markers.houses).each(function(index, elem){
        //if a house has one of the selected features from 'features' select box - show it on the map, otherwise hide it
        if(jQuery.inArray(features, elem.features) !== -1 || jQuery.inArray(features, elem.features) > -1){
            this.setVisible(true);
        }else{
            this.setVisible(false);
        }
    });

    // ... repeat the same for condos type elements ...
});

Update:

JS/jQuery code for creating/putting the markers on the map:

$('#map').gmap(mapOptions).bind('init', function(){             
    $(markers.houses).each(function(index, elem){
        $('#map').gmap('addMarker', {
            'position': new google.maps.LatLng(parseFloat(elem.lat), parseFloat(elem.lng)),
            'bounds': true,
            'icon': 'house.png'
        });
    });
    $(markers.condos).each(function(index, elem){
        $('#map').gmap('addMarker', {
            'position': new google.maps.LatLng(parseFloat(elem.lat), parseFloat(elem.lng)),
            'bounds': true,
            'icon': 'condo.png'
        }); 
    });
});
Was it helpful?

Solution

$(markers.houses).each(function(index, elem){
    //if a house has one of the selected features from 'features' select box - show it on the map, otherwise hide it
    if(jQuery.inArray(features, elem.features) !== -1 || jQuery.inArray(features, elem.features) > -1){
        this.setVisible(true);
    }else{
        this.setVisible(false);
    }
});

In your context, 'this' is not a marker element, it's a markers.houses array element, same as elem -> this === elem.

UPDATE: Add the markers with these lines:

$('#map').gmap(mapOptions).bind('init', function(){     
    markers.housesMarkers = [];     //Adding new property that holds Markers    
    $(markers.houses).each(function(index, elem){
            var position =  new google.maps.LatLng(parseFloat(elem.lat), parseFloat(elem.lng));
            var m = new google.maps.Marker({ position:position ,
                                            icon: 'house.png' })
            //Adding markers to array
            markers.housesMarkers.push( m );
            m.setMap( $('#map').gmap('get','map') );

            $('#map').gmap('addBounds',m.getPosition());  ///'bound:true' option
    });
    markers.condosMarkers = [];      //Adding new property that holds Markers
    $(markers.condos).each(function(index, elem){
            var position =  new google.maps.LatLng(parseFloat(elem.lat), parseFloat(elem.lng));
            var m = new google.maps.Marker({ position:position ,
                                            icon: 'condo.png' })
            //Adding markers to array
            markers.condosMarkers.push( m );
            m.setMap( $('#map').gmap('get','map') );

            $('#map').gmap('addBounds',m.getPosition());  ///'bound:true' option
    });
});

Then 'filter' (visible/invisible) your markers with this code:

$(markers.houses).each(function(index, elem){
    //if a house has one of the selected features from 'features' select box - show it on the map, otherwise hide it
    if(jQuery.inArray(features, elem.features) !== -1 || jQuery.inArray(features, elem.features) > -1){
        markers.housesMarkers[index].setVisible(true);   //for condos -> 'marker.condosMarkers'
    }else{
        markers.housesMarkers[index].setVisible(false);  //for condos -> 'marker.condosMarkers'
    }
});

OTHER TIPS

Try this.setMap(null); / this.setmap(map);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top