简体   繁体   中英

OpenLayers after load can't draw

I did function which draw and save it to 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. When I use getFeatures() it return array of features

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM