Restoring google map state when navigating back to map page
I have a Google map with markers which link through to other pages. If I click one of these markers, then decide to go back to the map page, the map position, zoom level, etc. is reset. Is there a simple way to restore the map to the state it was left at?
One Solution collect form web for “Restoring google map state when navigating back to map page”
The easiest and safest way to do this is to take the state you care about preserving (such as zoom, map centre, map type) and storing it somewhere semi-permanent in the browser. Depending on what browsers you want to support, your options are:
- Cookies (JS access via
- HTML5 local storage (modern browsers)
- URL hash tags (add
#zoom=4&...to the end of the page, kinda ugly, not really suitable)
- HTML5 history API (probably not suited either)
Cookies are probably the best way forward, maybe combined with local storage.
Note that it’s probably the browser
unload event causing this issue. However, it looks like the API itself is registering this event at the moment – so you can’t really avoid it.