I am working on a navigation thing that show next/previous views. I am storing the map's current zoom, center and rotation to an array object. But I want manage that history and navigation in a efficient manner.And I don't want to use the browser's history.

Is there any default methods or functionalities in Openlayers 3 to provide the prev/next thing ?


For my map I'm using UndoManager. You can store your state based on what you need but, after each navigation action you need to clear the vector and add the features you have stored in the state with before/after - depending on the situation.

This is how I store my state:

var addUndoRedo = function (type) {
        undoManager.add(new StateObject({
            vectorLayer: sourceVec,
            featuresBefore: featuresBefore, // this should be a cloned object of sourceVec.getFeatures() after draw/modify/remove
            featuresAfter: getFeatures(), // this should be a cloned object of sourceVec.getFeatures() at the time you call addUndoRedo()
            type: type // add,remove,modify,etc

StateObject should be able to performUndo() and performRedo() which is basicall clearing the vector and replacing the with before/after data by case.

 // an example of case when you add a navigation level
 sourceVec.once("addfeature", function () {
            //extra code you need to run

And this is how you undo/redo.

 if (undoManager.hasUndo()) {undoManager.undo();}
 if (undoManager.hasRedo()) {undoManager.redo();}

And if you need to reset for example you can always do :


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.