检测何时映射 'reclusters' 个特征

Detect when map 'reclusters' features

当地图 "reclusters" 时,我需要 运行 一些代码,请参阅此 GIF 了解我的意思。

有没有办法知道这种情况何时发生,或者有某种事件可以监听?

类似于:

map.on('cluster', function() {
   //code here
})

我目前的解决方案是运行我的代码:

map.on('zoom', () => {
    //my code
})

通过 运行 在 zoom 事件上使用我的代码,每次用户放大或缩小时我的代码 运行s,尽管我只需要它 运行 当地图正在聚类时。有什么可以在这里做的吗?提前致谢

设法找到解决方案,可能不是最好的,但以下代码对我有用:(简化)

// to keep track of the amount of clusters rendered
var previousClusterCount = 0

function foo() {
  //get all currently rendered clusters
  clusters = map.querySourceFeatures('your-cluster-layer')

  // if the amount of rendered clusters is different from the previous cluster count
  // then it means that the user has zoomed enough to trigger a "reclustering"
  // as shown in the GIF above
  if (clusters.length !== previousClusterCount) {
    //update cluster count
    previousClusterCount = clusters.length
    //run some code
  }
}

// set callback on zoom
map.on('zoom', foo)