2017-01-24 39 views
0

我必須加載某些功能在矢量層,並有風格的函數。OL3:聚類矢量功能時無法確定功能型

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什麼樣的幾何形狀。問題是:當我收集的類型是「點」我可以羣集,但與其他任何類型的,我不能看到圖層...我怎麼能聚點,而忽略多邊形和線條?還是讓OL3足夠聰明來決定?

編輯:我讀過https://github.com/openlayers/openlayers/pull/4917

+0

所以,你想獲得所有的功能,並單獨檢查它們以將它們添加到矢量源中,以防它們是點,並且如果它們是別的東西就完全忽略它們,對嗎?或者你想用其他功能做其他事情嗎? – Icarus

+0

不,我有一個帶FeatureCollection的GeoJSON字符串。現在我們可以假定所有幾何都是相同的類型。問題是我必須設置源之前,我知道我有什麼樣的幾何形狀的集合中並不能確定,如果我可以在我的'vectorLayer'使用'clusterSource'或'vectorSource'。但是......(不是主要問題),將是巨大的,如果我能得到的混合和的FeatureCollection只聚集點留下其他幾何形狀不變。 –

+0

閱讀https://github.com/openlayers/openlayers/pull/4917後我幾乎沒有,但仍然不能有條件地選擇'vectorSource'或'clusterSource'附加到'vectorLayer'。 –

回答

1

我會建議你創建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對象的幾個特徵,並根據幾何類型將它們添加到不同的來源。如果您希望在集羣中查看更多點以確保其正常工作,那麼也可以使用註釋行。

+0

你是個天才!好的解決方案像魅力一樣工作。 –