我正在尝试根据用户从选择框中选择的功能类型在我的谷歌地图上显示/隐藏(或简单地过滤)我的标记(房屋和公寓)。#features
例如,如果用户选择“游泳池”功能并单击Submit
按钮 - 仅显示具有此功能的标记(房屋/公寓),并隐藏没有游泳池的标记。
不幸的是,当我运行我的代码时,我的地图上什么都没有发生/变化。
房屋和公寓的JSON(存储在标记变量中):
({
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代码:
$('#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 ...
});
更新:
用于在地图上创建/放置标记的JS/jQuery代码:
$('#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'
});
});
});