Openlayers 3過濾加載geojson數據/逐條加載數據,並賦予屬性


近日有需求在openlayers 3中加載geojson數據,只加載指定屬性值得部分。

解決方法:

(1)ajax請求geojson數據

(2)逐條判斷屬性值,滿足條件的創建geometry

(3)滿足條件的創建feature並賦予屬性

(4)將所有滿足條件的features加入vector Layer

 

代碼如下:

function addFeaturesByAttributes(){    
    var myurl = "./resources/data/01.geojson";
    var source = new ol.source.Vector();
    $.ajax({
           url: myurl, 
           type: "GET",//請求方式為get
           dataType: "json", //返回數據格式為json
           async: false,//設置ajax為同步請求, 默認為異步
           success: function(data) {
               
                for(var i =0;i<data.features.length;i++){
                    if(data.features[i].properties.group == 4){//此處加載過濾條件,group為4的添加到圖層中
                        var coords = data.features[i].geometry.coordinates;
                        var geom = new ol.geom.Point(ol.proj.transform(coords, 'EPSG:4326', 'EPSG:3857'));
                        var feature = new ol.Feature(geom);
                        feature.setProperties({"name":"test","value":"test_value"});//為每個feature增加屬性字段及值
                        
                        source.addFeature(feature);
                    }
                }
           }
        }).responseText;
    var vector = new ol.layer.Vector({
        source: source,
        style:testStyleFunction
      });

    map.addLayer(vector);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM