This is documentation of a bèta release.
For documentation on the current version, please check Knowledge Base.

Restoring Viewer State

Closing your host application usually means that the viewer component is destroyed and loses all state. The SDK offers a mechanism to a) be notified when you should save viewer state and b) restore the viewer using that state. These can be used to persist viewer state across host application sessions.

This “state” object is made available as a string.

Saving state

To listen for state changes, we listen to the onAppStateChanged signal of the viewer. Save state whenever it changes.

Below is a pseudocode example of storing application state in the browser's localStorage.

// Listen to state changes
viewer.onAppStateChanged.add(handleAppStateChanged);
 
// Save state
function handleAppStateChanged(state) {
    window.localStorage.getItem("m3dviewer_state",state);
}

Loading state on startup

When creating the viewer, you have the ability to pass startup options. The available options are defined by the Constants class. You can use the Constants.STARTUP_APP_STATE startup option to restore a previously saved state.

Startup options can be provided only once, on viewer construction.

Below is a pseudocode example of restoring the application state using the state we have previously stored in the browser's localStorage.

// Get a reference to some API elements we will use.
var Constansts = orbitgt.mapping3d.sdk.viewer.Constants;
var SDKViewer = orbitgt.mapping3d.sdk.viewer.SDKViewer;
var AMap = orbitgt.mapping3d.sdk.viewer.AMap;
 
// Prepare options and set state.
var options = new AMap();
options.set(Constants.STARTUP_APP_STATE,window.localStorage.getItem("m3dviewer_state"));
 
// Create viewer
var appElement = document.getElementById("m3dviewer");
var viewer = new SDKViewer("example application",appElement,options);
 
Last modified:: 2020/05/15 11:32