0
votes

I have an OpenLayers.Layer.Vector layer that I allow the user to create, modify and delete features and feature attributes. The changes are saved when they hit a "Save Changes" button. If the user creates a new feature, then zooms the map out a ways, this causes OpenLayers to remove all features from the layer and only add in features saved to the GeoServer db. I have tried hanging onto the newly created features and adding them back to the layer on the "loadend" event but OpenLayers has destroyed the geometry of the new features so they are useless. How do I prevent OpenLayers from nuking the new features when zooming out?

1
Here's the workaround I'm currently using: in the layer "loadstart" handler, remove all of the new features and in the layer "loadend" handler, re-add all of the new features. OpenLayers should not remove features with a state of INSERT when requerying the db imho.Hilo
How do you add features to openlayers? It sounds like you're drawing them by hand instead of adding them to the map.mrówa
The new features are drawn by the user using the OpenLayers drawing controls which adds them to the layer.Hilo
I don't know why OpenLayers is removing your new features, it does not do so out of the box. Please refer to the openlayers.org/dev/examples/draw-feature.html <- it works here. Could you try stripping the code to the minimum and post it here?mrówa
If you look at the code for OpenLayers.Strategy.BBOX you'll see that it does do so "out of the box". When you zoom out, the layer is requeried with the new BBOX and calls OpenLayers.Strategy.BBOX.merge() which calls this.layer.destroyFeatures() thus removing/destroying all current features and adding back only those features saved to the database.Hilo

1 Answers

-1
votes

I've used the featuresremoved event to get all the removed features then I verify if it is an "Insert" state. To prevent multi-insertions on a multiple zoom-out I gave the feature an intermediate state. and made the insertions on loadend, changing the state to "Insert" again. Please note that I have only one editing layer a time.

var nuevas_features = null;
....
....
....
....
eventListeners: {

'loadstart': function(evt) {
    nuevas_features = null;

},
'featuresremoved' : function(algunfeature) {

    nuevas_features = null;
    nuevas_features = new Array();

    $(algunfeature.features).each(function(index, feature)
    {
           if(feature.state === 'Insert' )
           {    
               var feature_clonada = feature.clone();
               feature_clonada.state = "transicion";
               nuevas_features.push(feature_clonada);
           }
    });
},
'loadend': function(evt) {

    $(nuevas_features).each(function(index, feature)
    {
           feature.state = "Insert";
           editingLayer.addFeatures(feature);
    });
    console.log('end');

}

}