Mapbox 如何按状态筛选功能
我想在我的地图中显示一个簇层,根据是否打开进行过滤。你怎么能做到?我应该创建两个层吗 一个带过滤器:Mapbox 如何按状态筛选功能,mapbox,mapbox-gl-js,Mapbox,Mapbox Gl Js,我想在我的地图中显示一个簇层,根据是否打开进行过滤。你怎么能做到?我应该创建两个层吗 一个带过滤器:filter[“has”,“opened”]另一个带过滤器:filter[“!”,[“has”,“opened”] export const clusterLayerOpened = { id: "clusters", type: "circle", source: "earthquakes", filter: ["has", "opened"], pain
filter[“has”,“opened”]
另一个带过滤器:filter[“!”,[“has”,“opened”]
export const clusterLayerOpened = {
id: "clusters",
type: "circle",
source: "earthquakes",
filter: ["has", "opened"],
paint: {
"circle-color": [ "step", ["get", "opened"], "#51bbd6",100,"#f1f075",750,"#f28cb1", ],
"circle-radius": ["step", ["get", "opened"], 20, 100, 30, 750, 40],
},
};
export const clusterLayerNoOpened = {
id: "clusters",
type: "circle",
source: "earthquakes",
filter: ["!", ["has", "opened"]],
paint: {
"circle-color": [ "step", ["get", "opened"], "#51bbd6",100,"#f1f075",750,"#f28cb1", ],
"circle-radius": ["step", ["get", "opened"], 20, 100, 30, 750, 40],
},
};
这是我的geojson:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"id": "ak16994521",
"mag": 2.3,
"time": 1507425650893,
"felt": null,
"tsunami": 0,
"opened": true
},
"geometry": {
"type": "Point",
"coordinates": [-151.5129, 63.1016, 0.0]
}
},
{
"type": "Feature",
"properties": {
"id": "ak16994519",
"mag": 1.7,
"time": 1507425289659,
"felt": null,
"tsunami": 0,
"opened": false
},
"geometry": {
"type": "Point",
"coordinates": [-150.4048, 63.1224, 105.5]
}
}
]
}
无需创建两个单独的层来根据点是否已打开进行过滤。下面是一些代码,显示了如何添加一个图层,该图层显示属性为“打开”:true的所有点,并隐藏属性为“打开”:false的所有点:
map.addLayer({
'id': 'opened',
'type': 'circle',
'source': 'points',
'paint': {
'circle-radius': 10,
'circle-opacity': ["match", ["to-string", ["get", "opened"]], 'true', 1 , 'false', 0, 0]
}
});
要改为显示属性为“打开”的所有点:false,可以将'circle-opacity'
表达式切换为:
["match", ["to-string", ["get", "opened"]], 'true', 0 , 'false', 1, 0]
这段代码使用了一些映射框。我已将文档链接到此处的每个相关表达式:,和
下面是一个JSFIDLE,其中两个层被添加到映射中:。带有“打开”:true
的点显示为红色,带有“打开”:false
的点显示为蓝色。请注意,您需要在指定位置添加自己的Mapbox访问令牌,以便查看结果。以下是一个屏幕截图,作为预览:
好的,但它的想法是在集群层中显示打开或未打开的层。当你在做的时候,有两个图层可以工作,但是当我放大的时候,不会分割。当我放大已打开和未打开的分界点时,如何做到这一点???通过“打开”或“未打开”,您是指GeoJSON中的属性(即“打开”:true或“打开”:false,如示例GeoJSON中所提供的),还是指由某种用户交互确定的属性?听起来好像你在用“opened”一词来表示用户点击了集群或其他东西。如果您能澄清这一点,这将非常有帮助:)同时,您可能会找到GeoJSON源的集群属性的文档,以及与扩展集群相关的这三个GL JS方法:。