Openlayers 3 - 聚类和线条的组合

3

我的目标是在视图中聚合许多坐标。这些点之间还可以有“连接”,我想将其显示为LineString。

我尝试了很多次,但无法找到将线路集成到聚类中的方法。

您可以在此处查看我的当前(稳定)状态: Fiddle

var clusterSource = new ol.source.Cluster({
  distance: 20,
  source: vectorDevices
});

如果这两个点“合并”到聚类中,如果连接消失或成为聚类本身的一部分,那就太好了。
有人能帮我吗?
我尝试将vectorLine作为Cluster的源添加到fiddle中。
var clusterSource = new ol.source.Cluster({
  distance: 20,
  source: [vectorDevices, vectorLine]
});

或通过 addFeature fiddle 实现

clusterSource.addFeature(vectorLine);
1个回答

1
我认为目前无法向集群中添加线条。Openlayers 3(截至版本3.14.2)仅允许对点进行聚类。如果您查看ol.source.Cluster的源代码,则会发现一个语句,它断言其中所有要素都需要是点。 (来源于https://github.com/openlayers/ol3/blob/v3.14.2/src/ol/source/clustersource.js
ol.source.Cluster.prototype.createCluster_ = function(features) {
  var length = features.length;
  var centroid = [0, 0];
  for (var i = 0; i < length; i++) {
    var geometry = features[i].getGeometry();
    goog.asserts.assert(geometry instanceof ol.geom.Point,
        'feature geometry is a ol.geom.Point instance');
    var coordinates = geometry.getCoordinates();
    ol.coordinate.add(centroid, coordinates);
  }
  ol.coordinate.scale(centroid, 1 / length);

  var cluster = new ol.Feature(new ol.geom.Point(centroid));
  cluster.set('features', features);
  return cluster;
};

我认为最好的方法是在点聚合时以某种方式将线图层的可见性设置为false。
编辑:然而,在下一个版本的ol中可能会有所改变,已经合并了一个拉取请求(https://github.com/openlayers/ol3/pull/4917),其中包含geometryFunction选项。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接