Mapbox.单击群集获取点列表

Ana*_*lov 8 javascript mapbox-gl-js

我使用Mapbox GL JS并遇到群集问题.我通过点击群集添加了一些我想要获得聚集点列表的图层.

map.on('click', function(e) {
    var cluster = map.queryRenderedFeatures(e.point, { layers: ["cluster"] });

    if (cluster.length) {
        // get clustered points here
        console.log(cluster[0]);
    }
});
Run Code Online (Sandbox Code Playgroud)

关于jsfiddle的工作示例 https://jsfiddle.net/L3hm8rur/

ash*_*ish 14

Mabox GL JS库现在支持该功能.

这是API文档 - https://www.mapbox.com/mapbox-gl-js/api/#geojsonsource#getclusterchildren

如何获得群集下的积分?

map.on('click',/* cluster layer id */ 'clusters', function (e) {

  var features = map.queryRenderedFeatures(e.point, { layers: ['clusters'] });
  var clusterId = features[0].properties.cluster_id,
  point_count = features[0].properties.point_count,
  clusterSource = map.getSource(/* cluster layer data source id */'cluster-source');

  // Get Next level cluster Children
  // 
  clusterSource.getClusterChildren(clusterId, function(err, aFeatures){
    console.log('getClusterChildren', err, aFeatures);
  });

  // Get all points under a cluster
  clusterSource.getClusterLeaves(clusterId, point_count, 0, function(err, aFeatures){
    console.log('getClusterLeaves', err, aFeatures);
  })

});
Run Code Online (Sandbox Code Playgroud)


mol*_*erp 7

编辑:这在mapbox-gl-js的最新版本中得到官方支持,因此您不需要使用我建议的解决方法.有关详细信息,请参阅其他答案.

不幸的是,目前不支持您正在寻找的行为.群集层不包含群集中各个点的数据.

解决方法是过滤GeoJSON源,以获得clusterRadius距离点击点不远的点,这将为您提供所需的点数.

JSFiddle:https://jsfiddle.net/aznkw784/

  • 免责声明 - 我在Mapbox工作