Features added to vector layer are not visible
Asked Answered
D

0

6

I am trying to add new features to a vector layer.

I do it with this code:

layer.getSource().addFeatures(features);

Where layer is an instance of ol.vector.Layer and features is an array of ol.Feature

The features are added but they are not visible. And unfortunately even calling

map.render()

does not help. The only solution that works is reloading all features for the layer:

layer.getSource().clear();
view = map.getView();
layer.getSource().loadFeatures(map.getExtent(), view.getResolution(), view.getProjection());

But it is quite annoying to wait for the whole layer reload each time you want to add a feature.

Any ideas what I might be doing wrong ?

Durrett answered 7/8, 2015 at 13:42 Comment(7)
How so you initialize the source? What options?Amalamalbena
new ol.layer.Vector({ visible: true, source: new ol.source.Vector({ format: new ol.format.GeoJSON(), strategy:ol.loadingstrategy.bbox, loader: prepareLoader() }); The prepareLoader() prepares my custom loading function for the layer.Durrett
Ok, so what does the prepareLoader do? It might be related. Mind pasting it's code?Amalamalbena
It is too long to put it inisde a comment. But the method creates a loader function for Vector layer. It only sends an asynchronous POST to the server to request the features in GeoJSON format. The loader function is managed by OL3 and is called when bbox is changed. So it does not take part in editing of the features. Or at least I think it has no affect on it. The POST method's success callback looks like this: var features = new ol.format.GeoJSON().readFeatures(response, {featureProjection: projection}); ol3Layer.getSource().addFeatures(features);Durrett
Ok. 1. Your loader adds the feature to the layer ol3Layer, but in your question you call the layer "layer". Is it just editing to make the question more readable, or is it possible that they reference different layers? 2. Do you set any styles on the features or the layer? 3. Are you sure that the features you add manually are in the same CRS as those loaded from the server?Amalamalbena
It is just to make it more readable. Layer and ol3Layer is the same - just different variable names. All styles are set properly and I can see the features. I will check the CRS though. Maybe I do something wrong there.Durrett
The newly created feature looks exactly like features that are already in the OL3 source of the layer. So I guess it is not caused by CRS or any lack of the feature. I will investigate further and I'll let you know if I find anything.Durrett

© 2022 - 2024 — McMap. All rights reserved.