openlayers 3中集群策略的门槛在哪里?

Where is threshold for cluster strategy in openlayers 3?

在 openlayers 2.8 中,根据票证 https://trac.osgeo.org/openlayers/ticket/1815

有一个与集群策略关联的阈值

在 openlayers 3 中没有任何地方提到它(而且策略范式似乎也消失了)。 http://openlayers.org/en/master/apidoc/ol.source.Cluster.html

有人知道这个功能是否有门票吗?

范式发生了很大变化。在 OpenLayers 3 中,您创建一个具有簇样式的新图层,并且 "threshold" 在图层选项中设置为 maxResolution 或 minResolution。 类似于:

var clusterLayer = new ol.layer.Vector({
                visible: true,
                zIndex: insightMap.totalServcies - element.SortOrder,
                id: Id,                                           
                serviceId: element.Id,
                minResolution: clusteringThreshold,
                cluster: true,
            });

您也可以根据文档使用 minZoom 和 maxZoom,但我遇到了它们始终如一地执行的问题。

更新

实际上可以在不重新编译库的情况下获得适当的集群阈值。您需要在样式函数中使用每个特征的几何形状(来自集群的 features 属性)。

const noClusterStyles = [];
vectorLayer.setStyle(feature => {
  const features = feature.get('features');
  if (features.length > 5) {
    return clusterStyle;
  } else {
    for (let i = 0; ii = features.length; i < ii; ++i) {
      const clone = noClusterStyles[i] ? noClusterStyles[i] : noClusterStyle.clone();
      clone.setGeometry(features[i].getGeometry());
      noClusterStyles[i] = clone;
    }
    noClusterStyles.length = features.length;
    return noClusterStyles;
  }
});

感谢@ahocevar for the code snippet

另一种解决方案需要修改 OL3 库本身。

ol.source.Cluster.prototype.cluster_函数有以下代码片段:

var neighbors = this.source_.getFeaturesInExtent(extent);
ol.DEBUG && console.assert(neighbors.length >= 1, 'at least one neighbor found');
neighbors = neighbors.filter(function(neighbor) {
  var uid = ol.getUid(neighbor).toString();
  if (!(uid in clustered)) {
    clustered[uid] = true;
    return true;
  } else {
    return false;
  }
});


// Add the following
// If one element has more too many neighbors, register it as a cluster of one 
// Size-based styling should be handled separately, in the layer style function

let THRESHOLD= 3;
if(neighbors.length > THRESHOLD) {
     this.features_.push(this.createCluster_(neighbors));
} else {
    for(var j = 0 ; j < neighbors.length ; j++) {
        this.features_.push(this.createCluster_([neighbors[j]]));
    }
}