【问题标题】:Mapbox. Get list of points by click on cluster地图盒。通过单击集群获取点列表
【发布时间】:2016-12-03 17:26:27
【问题描述】:

我使用 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]);
    }
});

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

【问题讨论】:

  • 更新 -- 此功能正在 github.com/mapbox/supercluster/pull/31 上积极开发

标签: javascript mapbox-gl-js


【解决方案1】:

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

很遗憾,您正在寻找的行为目前不受支持。聚类层不包含聚类中各个点的数据。

一种解决方法是过滤您的 GeoJSON 源中的点,以查找距单击点的 clusterRadius 距离内的点,这将为您提供您正在寻找的点。

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

  • 免责声明 - 我在 Mapbox 工作

【讨论】:

【解决方案2】:

正如@mollymerp 前面提到的,supercluster 具有方法getChildren(clusterId, clusterZoom),它将从集群中返回子节点。


编辑

https://jsfiddle.net/denistsoi/bhzr6hpt/3/

对于上面的示例 - 我使用了 getLeaves 方法,但最好系统地调用 getChildren 并在每个连续的缩放级别上磨练以确定相应集群中存在的内容


// GEOJSON being the valid geojson source
map.addSource('data', { 
   type: 'geojson', 
   data: [GEOJSON] 
}); 

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

  if (cluster.length) {
    // load values to determine cluster points & children

    var superc = supercluster({
       radius: 40,
       maxZoom: 16
    })

    superc.load(map.getSource('data').serialize().data.features);

    var children = superc.getChildren(0, map.getZoom());
    console.log(children); // returns array of children from clustered point;
  }
});

【讨论】:

  • 试过你的答案,但获得超集群不是一个功能
  • 我忘记将 params 对象添加到超集群 - 这可能会解决您的问题@Prince
  • 请访问我的代码链接stackoverflow.com/questions/45003613/…
  • 即使更新为超级集群,我仍然得到超级集群不是一个函数({ radius: 40, maxZoom: 16 })
  • 给我一个 jsfiddle 链接 - (也放一些数据在那里) - 我刚刚删除了一个额外的括号;也可能在那里造成问题
【解决方案3】:

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);
  })

});

【讨论】:

    【解决方案4】:
               clusterSource.getClusterLeaves(clusterId, pointCount, 0, function (error, features) {
                    // Print cluster leaves in the console
                    console.log('Cluster leaves:', error, features);
    
                    //build the bounding box with the selected points coordinates
                    bounds = new (mapboxgl.LngLatBounds)();
                    features.forEach(function (feature) {
                        bounds.extend(feature.geometry.coordinates);
                    });
                    //Move the map to fit the Bounding Box (BBox)
                    return map.fitBounds(bounds, {
                        padding: 45, //orig 45
                        maxZoom: 16
                    });
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 2022-01-09
    相关资源
    最近更新 更多