You may find it useful to keep track of the state of a map, such as its scale level, or the location it displayed. If you save the map state, users can reload the web page, or leave the page and return to it later at the same map position.

You can use the Map.saveState() method to store the state, and the Map.restoreState() method to restore it afterward.

In this example, those methods store the state in a cookie, and restore it from the cookie later on:

Program: Saving and restoring map state (from samples/cop/WorkspaceManager.js)
// Get the state from the map and store it in a cookie.
currentState.mapState = map.saveState();
Cookie.set(getCookieName(), JSON.stringify(currentState), 200);

// Retrieve the map state from the cookie (if it exists) and restore the map.
var myJsonCookie = Cookie.get(getCookieName());
if (myJsonCookie) {
  currentState = JSON.parse(myJsonCookie);
} else {
  currentState = {};

if (currentState.mapState) {

You can’t restore a 2D state on a 3D map, nor a 3D state on a 2D map.