OL3:无法确定特征类型时的聚类矢量特征

OL3: Clustering Vector Features when can't determine Feature type

我必须在矢量图层中加载一些功能并具有样式功能。

var features = new ol.format.GeoJSON().readFeatures( geojsonStr, {
    featureProjection: 'EPSG:3857'
});         

var vectorSource = new ol.source.Vector({
    features: features,
}); 


/*
var clusterSource = new ol.source.Cluster({
    distance: 15,
    source: vectorSource
});
*/          

var customStyleFunction = function( feature, resolution ) {
   ....
}

var vectorLayer = new ol.layer.Vector({
    //source: clusterSource,
    source: vectorSource,
    style : customStyleFunction
});

map.addLayer( vectorLayer );

不知道geojsonStr会得到什么样的几何体。问题是:当我的 collection 是 "Point" 类型时,我可以将它聚类,但是对于任何其他类型我看不到图层...我如何聚类点并忽略多边形和线?还是让OL3聪明点来决定?

编辑: 我读过 https://github.com/openlayers/openlayers/pull/4917

我建议您创建 2 个不同的层:一个用于聚类,另一个用于公共向量层。

要解决您的问题,您可以遍历要素并检查每个要素的几何类型,然后使用 addFeature 方法将其添加到现有源中:

for (var i = 0; i < geojsonFeatures.length; i++) {
    if (geojsonFeatures[i].getGeometry().getType() === 'Point') {
        clusterSource.addFeature(geojsonFeatures[i]);
    } else {
        vectorSource.addFeature(geojsonFeatures[i]);
    }
}

我创建了一个 jsfiddle,它从 GeoJSON 对象获取几个特征,并根据几何类型将它们添加到不同的源。如果您想在集群源中查看更多点以确保其正常工作,您也可以使用注释行。