Quick Tip: Save last location on map reload
I’d like to
I recently had a need in an ArcGIS JS project to save a maps last location when the user closed the browser so that it would return that location when they returned to the application. After a little trial and error, I think I came up with a pretty good solution.
Basically you’ll want to use localStorage to store the map center and zoom level. Then when the map reloads, read from localStorage and use the maps centerAndZoom method to return to that location. You can capture the page close and refresh using window.onbeforeunload or since you’re already using Dojo, use dojo/_base/unload.
I put together a JSBIN example for this. Simple Map
Pan around the map and refresh the page. You should be returned to the last known location. This is coming in pretty handy so far. You can modify this to add more options and the ability to disable it, but I’ll leave those details up to you.
I updated the sample to sniff the device and use the extent-change event if on an iOS device, since it seems that the window.onbeforeunload event doesn’t behave as expected on iOS devices.
I’ve incorporated this into my map-widget library and put together a demo from my esri-js-starterkit with this feature enabled.