如何向Mapbox地图添加过滤器?

时间:2019-01-24 17:05:57

标签: mapbox mapbox-gl-js mapbox-gl

我正在尝试向Mapbox地图添加一些基本过滤器。该地图正在从geojson数据文件中提取数据。我要过滤的数据字段是“卧室数量”和“价格范围”。卧室数据实际上仅为0-5,价格范围是基本数字格式:1000、2500、4000。理想情况下,卧室只需一个简单的单选按钮和价格范围滑块即可。到目前为止,我在这里尝试了Mapbox复选框示例:https://docs.mapbox.com/mapbox-gl-js/example/filter-markers/,但这不适用于geojson数据url源。我还尝试了单选按钮https://docs.mapbox.com/help/tutorials/show-changes-over-time/的示例。这引用了一个日期范围,尽管它不适用于我的用例。我还有其他例子可以参考吗?我简要地研究了setfilter函数,但是没有运气。我真的在这个问题上苦苦挣扎,因此任何提示或想法都将有所帮助。预先感谢!

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Test</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user- 
scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox- 
gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.css' 
rel='stylesheet' />
<style>
body { margin:0; padding:0; height:500px;}
#map { position:absolute; top:0; bottom:0; width:91%; height:500px;}
img {
height: 100px;
width: 120px;
}
.mapboxgl-ctrl-compass {
display: none !important;
}
</style>
</head>
<body>

<div id='map'></div>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl- 
geocoder/v2.3.0/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl- 
js/plugins/mapbox-gl-geocoder/v2.3.0/mapbox-gl-geocoder.css' type='text/css' 
/>
<script>
mapboxgl.accessToken = 'mycode12345';
var address = localStorage.getItem('address'); //local storage item from 
previous page
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v9',
center: address, //How would I pass it to the map center?
zoom: 3
});

map.on('load', function() {
map.addSource("Properties", {
type: "geojson",
data: "mydataurl",
cluster: true,
clusterMaxZoom: 14, 
clusterRadius: 50
});

map.addLayer({
id: "clusters",
type: "circle",
source: "Properties",
filter: ["has", "point_count"],
paint: {
"circle-color": [
"step",
["get", "point_count"],
"#ffffff",
100,
"#ffffff",
750,
"#ffffff"
],
"circle-radius": [
"step",
["get", "point_count"],
20,
100,
30,
750,
40
]
}
});

map.addLayer({
id: "cluster-count",
type: "symbol",
source: "Properties",
filter: ["has", "point_count"],
layout: {
"text-field": "{point_count_abbreviated}",
"text-font": ["DIN Offc Pro Medium", "Arial Unicode MS Bold"],
"text-size": 12
}
});

map.addLayer({
id: "unclustered-point",
type: "circle",
source: "Properties",
filter: ["!", ["has", "point_count"]],
paint: {
"circle-color": "#ffffff",
"circle-radius": 8,
"circle-stroke-width": 1,
"circle-stroke-color": "#fff"
}
});

map.on('click', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['unclustered-point'] // replace this with the name of the layer
});

if (!features.length) {
return;
}

var feature = features[0];

var popup = new mapboxgl.Popup({ offset: [0, -15] })
.setLngLat(feature.geometry.coordinates)
.setHTML('myhtml')
.setLngLat(feature.geometry.coordinates)
.addTo(map);
});
});

map.on('mouseenter', 'clusters', function () {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'clusters', function () {
map.getCanvas().style.cursor = '';
});
map.addControl(new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
placeholder: "Location",
animate: false,
zoom: 7.2
}), 'top-left');
map.addControl(new mapboxgl.NavigationControl(), 'bottom-right');
</script>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我目前正在一个项目中,我有一个包含多边形的geoJSON和一个属性数据库(包括标记的经/纬度位置)。我对您正在执行的操作的解决方案如下:

https://docs.mapbox.com/mapbox-gl-js/example/toggle-layers/

当我使用本教程打开和关闭图层(geoJSON图层)时,我包含了一个新功能,该功能可以对数据和结果进行单独过滤

    if (visibility === 'visible') {
        map.setLayoutProperty(clickedLayer, 'visibility', 'none');
        this.className = '';
    } else {
        this.className = 'active';
        map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
        if ( clickedLayer == 'Corn' ){
        FilterCorn();
    }if ( clickedLayer == 'MultiPoly' ){
        FilterMulti();
    }
    }

例如,如果我打开包含显示玉米数据的多边形的图层,则调用此函数,该函数可以确定图层要素(1个要素= 1个多边形)中是否存在点(标记)。

 function FilterCorn() { 

    $(function() {
      var cluster = [];
      var features = [];
      var point = turf.point([-76.161496, 45.4734536]); 
      var url = '/geoJSON/Corn.json';

      $.getJSON(url, function(data)
      {
        features = data.features;

        for (var i = 0, len = features.length; i < len; i++)
        {
          // Get coordinates
          var is_inside = turf.inside(point, features[i]);
          console.log(is_inside);

          if (is_inside) {
            console.log('Found it inside');
          }
        }

        console.log(cluster);
      });
    });

如果您可以提供小提琴,我将很乐意对其进行编辑以执行您想要的操作。