【问题标题】:how to add multiple filters to mapbox leaflet maps如何向 mapbox 传单地图添加多个过滤器
【发布时间】:2014-10-29 18:27:07
【问题描述】:

我有一个有效的 mapbox/leaflet 地图,我可以根据下拉菜单进行过滤,但只有其中一个可以工作,不确定组合过滤器的语法(或者是否可能)?

我基本上有一个房地产地图,其中包含属性类型和社区的 json 数据。需要组合可能的过滤器,因此选择不同的属性类型不会删除邻域过滤器。

$('#propertytype').change(function() {
    if ($(this).val() === 'all') {
        console.log($(this).val());
        markers.setFilter(function(f) {
            return f.properties['type'] != null;
        });
    } else {
        console.log($(this).val());
        var ptype = $(this).val();
        markers.setFilter(function(f) {
            return f.properties['type'] === ptype;
        });
        return false;
    }
});

$('#neighborhood').change(function() {
    if ($(this).val() === 'all') {
        console.log($(this).val());
        markers.setFilter(function(f) {
            return f.properties['neighborhood'] != null;
        });
    } else {
        console.log($(this).val());
        var hood = $(this).val();
        markers.setFilter(function(f) {
            return f.properties['neighborhood'] === hood;
        });
        return false;
    }
});

【问题讨论】:

    标签: javascript map filter leaflet mapbox


    【解决方案1】:

    当然,简化为一个函数:

    $('#propertytype, #neighborhood').change(function() {
        var propertyType = $('#propertytype').val();
        var neighborhood = $('#neighborhood').val();
        markers.setFilter(function(f) {
            return (propertyType === 'all' || f.properties.type == propertyType) &&
                (neighborhood === 'all' || f.properties.neighborhood === neighborhood);
        });
        return false;
    });
    

    (尚未执行此代码,但应该可以)

    【讨论】:

    • 完美!这就是我想要做的,但不知道它是如何在 javascript 中完成的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多