簡體   English   中英

加載后的OpenLayers無法繪制

[英]OpenLayers after load can't draw

我做了繪制和保存到GeoJson的功能。 然后我得到了: 保存結果

接下來,當我加載要素時可以按時進行繪制,但出現錯誤:“ layer.getSource()。getFeaturesCollection()” == null,但是我使用FeatureCollection類型保存了特征。 當我使用getFeatures()時,它返回功能數組

加載層的開始:

if(map_info != "") {
            var layer_area = new ol.layer.Vector({
                source: new ol.source.Vector({
                    features: (new ol.format.GeoJSON()).readFeatures(map_info['layers'][0])
                }),
            });
            var layer_branch = new ol.layer.Vector({
                source: new ol.source.Vector({ features: (new ol.format.GeoJSON()).readFeatures(map_info['layers'][1]) })
            });
            var layer_region = new ol.layer.Vector({
                source: new ol.source.Vector({ features: (new ol.format.GeoJSON()).readFeatures(map_info['layers'][2]) })
            });
            var layer_ambon = new ol.layer.Vector({
                source: new ol.source.Vector({ features: (new ol.format.GeoJSON()).readFeatures(map_info['layers'][3]) })
            });

            var view=new ol.View({
                center: [{{map.pos_x}}, {{map.pos_y}}],
                zoom: {{map.zoom}}
            });
        }else {
            var layer_area = new ol.layer.Vector({
                source: new ol.source.Vector({ features: new ol.Collection() })
            });
            var layer_branch = new ol.layer.Vector({
                source: new ol.source.Vector({ features: new ol.Collection() })
            });
            var layer_region = new ol.layer.Vector({
                source: new ol.source.Vector({ features: new ol.Collection() })
            });
            var layer_ambon = new ol.layer.Vector({
                source: new ol.source.Vector({ features: new ol.Collection() })
            });
            var view=new ol.View({
                center: [2097096.387839827, 6887410.172738342],
                zoom: 5
            });
        }

繪圖功能:

$("#draw_ambon").on("click", function(){
    var layer=manager["ambon"];
    map.removeInteraction(draw);
    select !== null ? map.removeInteraction(select) : 0;
    setInteraction(layer.getSource().getFeaturesCollection(), "Point");
    layer.setStyle(pointStyleFunction);
    layer.setMap(map);

    draw.on("drawend", function(e){
        layer.getSource().addFeatures(e.feature);
        e.feature.setProperties({
            'id': layer.getSource().getFeaturesCollection().getLength(),
            'type': "ambona",
            'name': ''
        })
    })
});

我解決了 只需將clas ol.Collection添加到源代碼中的features選項:

source: new ol.source.Vector({ features: new ol.Collection(new ol.format.GeoJSON()).readFeatures(map_info['layers'][1])) })

暫無
暫無

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

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