简体   繁体   English

加载后的OpenLayers无法绘制

[英]OpenLayers after load can't draw

I did function which draw and save it to GeoJson. 我做了绘制和保存到GeoJson的功能。 I got then: result from save 然后我得到了: 保存结果

Next when I load features it's ok to time when I want draw, I got error: "layer.getSource().getFeaturesCollection()" == null but I saved fetures with type featureCollection. 接下来,当我加载要素时可以按时进行绘制,但出现错误:“ layer.getSource()。getFeaturesCollection()” == null,但是我使用FeatureCollection类型保存了特征。 When I use getFeatures() it return array of features 当我使用getFeatures()时,它返回功能数组

Begin of load layers: 加载层的开始:

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 functions: 绘图功能:

$("#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': ''
        })
    })
});

I resolved it. 我解决了 Just add clas ol.Collection to features option in source: 只需将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