1

我正在尝试根据用户从选择框中选择的功能类型在我的谷歌地图上显示/隐藏(或简单地过滤)我的标记(房屋和公寓)。#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'
        }); 
    });
});
4

2 回答 2

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'
    }
});
于 2012-06-13T18:29:21.403 回答
0

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

于 2012-06-13T18:22:29.943 回答