簡體   English   中英

如何獲取開放層3中向量層內的向量元素數

[英]How to get the number of vector elements inside a vector layer in open layers 3

有人知道如何對OL3向量層中存在的要素數量進行計數嗎?

我的矢量層的定義如下,我想知道它有多少個元素,理想情況下當前正在渲染多少個元素:

var styleCache = {};
var WFS_layer_Traffic_Lights = new ol.layer.Vector({
source : new ol.source.GeoJSON({
    projection : 'EPSG:3857',
    url : "Vector_Data/Traffic_Lights_Bordeaux.geojson"
}),


style : function(feature, resolution) {

    var path;
    var x_anchor;
    var y_anchor;       

    if(resolution < 4){
    path = 'Icons/Traffic_Lights_Sign_Icon_Small.png';
    x_anchor = 23;
    y_anchor = 90;
    }
    if(resolution >= 4 && resolution < 10){
    path = 'Icons/Traffic_Lights_Sign_Small.png'; 
    x_anchor = 16;
    y_anchor = 16;
    }       
    if(resolution >= 10){
    path = 'Icons/Traffic_Lights_Sign_Tiny.png';        
    x_anchor = 10;
    y_anchor = 10;
    }


    if (!styleCache[path]) {
        styleCache[path] = [new ol.style.Style({
            fill : new ol.style.Fill({
                color : 'rgba(255, 255, 255, 0.1)'
            }),
            stroke : new ol.style.Stroke({
                color : '#319FD3',
                width : 1
            }),
            image: new ol.style.Icon(({
                    anchor: [x_anchor, y_anchor],
                    anchorXUnits: 'pixels',
                    anchorYUnits: 'pixels',
                    src: path
                })),
            text : new ol.style.Text({
                font : '12px Calibri,sans-serif',
                text : "",
                fill : new ol.style.Fill({
                    color : '#000'
                }),
                stroke : new ol.style.Stroke({
                    color : '#fff',
                    width : 4
                })
            }),
            zIndex : 1
        })];
    }
    return styleCache[path];
}
});

加載(GeoJSON)功能后,您可以在向量源上調用getFeatures以獲取包含對向量源中包含的功能的引用的數組。 因此,要獲得功能數量,可以使用以下命令:

var featureCount = vectorLayer.getSource().getFeatures().length;

如上所述,應加載源以使其正常工作。 如果將url選項傳遞給源構造函數,則源將使用Ajax請求下載功能。 這是異步發生的,這意味着源在構造后將不包含要素。

您可以在矢量源上注冊一個change偵聽器,以了解何時加載:

var vectorSource = vectorLayer.getSource();
var listenerKey = vectorSource.on('change', function(e) {
  if (vectorSource.getState() == 'ready') {
    var featureCount = vectorSource.getFeatures().length;
    // ...
    ol.Observable.unByKey(listenerKey);
    // use vectorSource.unByKey(listenerKey) instead
    // if you do use the "master" branch of ol3
  }
});

編輯:我編輯此更改從change:state change為事件名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM