我们现在可以在gl-js中对标记进行聚类:
https://www.mapbox.com/mapbox-gl-js/example/cluster/
在mapbox-gl-js中有没有一种等价的标记过滤器的集群方式,就像下面这个例子一样:
https://www.mapbox.com/mapbox.js/example/v1.0.0/filtering-marker-clusters/
发布于 2016-05-07 02:51:14
我也遇到过这个问题,我相信应该有一种方法来根据集合中要素的属性进一步过滤聚类层。但根据我的理解(我真心希望有一种更好的方法,只是我还没有弄明白),没有一种方法可以区分集群的功能,因为你在源代码上声明它是集群的。我想出的一个解决办法是随着过滤器的变化动态添加源和层。
例如:如果您正在按子类别ID过滤,则可以减少与该子类别ID匹配的原始FeatureCollection,并使用该FeatureCollection创建一个新的源并将其设置为true,然后添加标记的层,然后添加簇层,如示例中所示。任何时候过滤器发生变化,你可以切换标记层的可见性(还没有测试该部分),或者只是移除它并添加新的标记层,重复前面的步骤。这种方法的许多缺点之一是无法在整个数据集上使用常规过滤器(如果您有的话),而且它的性能不如使用过滤器时那么好。
如果使用mapbox中的地震数据,代码可能如下所示:
var data = "https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson";
var filterID = 1.87; // Arbitrary number
var paredData = _.filter(data.features, function(feature){
if (feature["Primary ID"] === filterID) {
return feature;
}
})
// Remove all of the layers and source associated
// with a previous filter if needed
if (map.getSource("earthquake-filter") {
map.removeLayer("earthquake-filter");
map.removeLayer("earthquake-filter-cluster");
map.removeLayer("earthquake-filter-cluster-count");
map.removeSource("earthquake-filter");
}
map.addSource("earthquake-filter", {
type: "geojson",
data: {
type: "FeatureCollection",
features: paredData
},
cluster: true,
clusterMaxZoom: 14,
clusterRadius: 50
});然后继续执行示例中的操作。
这不是我最喜欢的解决方案,但到目前为止这是我找到的唯一有效的解决方案。希望这能有所帮助。
https://stackoverflow.com/questions/36179464
复制相似问题