我试图根据用户从{{1>中选择的功能类型,在我的 Google地图上显示/隐藏(或简单地过滤)我的标记(房屋和公寓)选择框。
例如如果用户选择“游泳池”功能并点击#features
按钮 - 只显示具有此功能的那些标记(住宅/公寓),以及隐藏那些没有游泳池的人。
不幸的是,当我运行代码时,我的地图上没有任何变化/变化。
房屋和公寓的JSON (存储在 标记 变量中):
Submit
JS / jQuery 代码:
({
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 ...
});
答案 0 :(得分:2)
$(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);
}
});
在您的上下文中,“this
”不是标记元素,它是markers.houses
数组元素,与elem
相同 - > this === elem
。
更新:使用以下行添加标记:
$('#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
});
});
然后使用以下代码'过滤'(可见/不可见)您的标记:
$(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'
}
});
答案 1 :(得分:0)
试试this.setMap(null); / this.setmap(map);