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/symbology/cop/COPStorageManager.ts)
const state = {
  activeTheme: theme.name,
  mapState: map.saveState()
}

const date = new Date();
date.setTime(date.getTime() + (expireDays * 24 * 60 * 60 * 1000));
const expires = "expires=" + date.toUTCString();
document.cookie = COP_COOKIE_NAME + "=" + encodeURIComponent(JSON.stringify(state)) + ";" + expires;
const cookie = loadCookie();
if (cookie) {
  const currentState = JSON.parse(cookie);
  if (currentState.mapState) {
    map.restoreState(currentState.mapState);
  }
}

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