Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
dev:developer:3dmapping_sdk:viewer_state [2020/05/15 11:22]
jeroen removed
— (current)
Line 1: Line 1:
-====== 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. 
- 
-<code javascript> 
-// Listen to state changes 
-viewer.onAppStateChanged.add(handleAppStateChanged); 
- 
-// Save state 
-function handleAppStateChanged(state) { 
-    window.localStorage.getItem("m3dviewer_state",state); 
-} 
-</code> 
- 
-===== Loading state on startup ===== 
- 
-When creating the viewer, you have the ability to pass [[glossary|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. 
- 
-<code javascript> 
-// 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); 
-</code>