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

This is an old revision of the document!


Restore Viewer State

By default closing your host application means that the viewer component is destroyed and loses all state. The SDK offers a mechanism to a) be notified when its state changes and b) restore the viewer using that state. These can be used to persist viewer state across host application sessions.

This allows you to make the viewer to remember state :

  • Remember last login
  • etc…

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

Listen for state changes and save them

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

// Listen to the state-changed signal
viewer.onAppStateChanged.add(handleAppStateChanged);

Restore the viewer using previous state

When creating the viewer, you have the ability to pass startup options.
The appState option can be used to provide a state to start from.

var options = new orbitgt.mapping3d.sdk.viewer.AMap();
options.set("appState",savedAppState);

Example

Below is a full javascript example using window.localStorage for storing state.
Try logging in to the viewer with “Remember me” checked, and refresh te page.

var viewer;
 
/**
 * Called when something changes to the viewer state. 
 */
function handleAppStateChanged(state) {
    // Save state somewhere...
    window.localStorage.setItem("m3dviewer_state",state);
}
 
/**
 * Called when the viewer component is ready for interaction.
 */
function handleReady() {
    // Listen to the state-changed signal
    viewer.onAppStateChanged.add(handleAppStateChanged);
}
 
/**
 * Called when the page is full loaded.
 */
function handleDOMReady() {
    // Create viewer startup options
    var options = new orbitgt.mapping3d.sdk.viewer.AMap();
    options.set("appState",window.localStorage.getItem("m3dviewer_state"));
    // Create viewer
    var appElement = document.getElementById("m3dviewer");
    viewer = new orbitgt.mapping3d.sdk.viewer.SDKViewer(appElement,options);
    viewer.setSize(600,400);
    viewer.isReady.then(handleReady);
}
 
// Wait for page to load
document.addEventListener("DOMContentLoaded", handleDOMReady);

 
Last modified:: 2018/06/22 16:32