javascript - Show/hide markers based on form's selections doesn't do anything -
i'm trying show/hide( or put - filter ) markers( of houses , condos ) on google map, based on types of features user selected #features
select box.
e.g. if user selectes 'swimming pool' feature , clicks submit
button - show markers( houses/condos ) have feature, , hide ones don't have swimming pool.
unfortunately, nothing happens/changes on map when run code.
json of houses , condos( stored inside markers variable ):
({ condos: [ {address:'123 fake st.', lat:'56.645654', lng:'23.534546', features:['swimming pool','bbq','..etc..']}, {... condo features ...}, {... condo features ...}, ... ], houses: [ {address:'1 main ave.', lat:'34.765766', lng:'27.8786674', features:['...','...']}, {... house features ...}, {... house features ...}, ... ] })
js/jquery code:
$('#filterform').on('submit', function(e) { e.preventdefault(); // prevent page reloading when form submitted $('#map').gmap('set', 'bounds', null); // reset map's bounds $('#map').gmap('closeinfowindow'); // close opened infowindows // store selected features 'features' select box features variable array var features = $("#features").val(); // each element of type house/inside houses array $(markers.houses).each(function(index, elem){ //if house has 1 of selected features 'features' select box - show on map, otherwise hide if(jquery.inarray(features, elem.features) !== -1 || jquery.inarray(features, elem.features) > -1){ this.setvisible(true); }else{ this.setvisible(false); } }); // ... repeat same condos type elements ... });
update:
js/jquery code creating/putting markers on 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' }); }); });
$(markers.houses).each(function(index, elem){ //if house has 1 of selected features 'features' select box - show on map, otherwise hide if(jquery.inarray(features, elem.features) !== -1 || jquery.inarray(features, elem.features) > -1){ this.setvisible(true); }else{ this.setvisible(false); } });
in context, 'this
' not marker element, it's markers.houses
array element, same elem
-> this === elem
.
update: add markers these lines:
$('#map').gmap(mapoptions).bind('init', function(){ markers.housesmarkers = []; //adding new property 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 array markers.housesmarkers.push( m ); m.setmap( $('#map').gmap('get','map') ); $('#map').gmap('addbounds',m.getposition()); ///'bound:true' option }); markers.condosmarkers = []; //adding new property 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 array markers.condosmarkers.push( m ); m.setmap( $('#map').gmap('get','map') ); $('#map').gmap('addbounds',m.getposition()); ///'bound:true' option }); });
then 'filter' (visible/invisible) markers code:
$(markers.houses).each(function(index, elem){ //if house has 1 of selected features 'features' select box - show on map, otherwise hide 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' } });
Comments
Post a Comment