Google Maps API Tutorial© 2008, 2009 Mike Williams
|Translate this page:|
Using CookiesIf you want your map to remember its state when the user leaves the page and comes back, then you can write the information into a cookie.
A cookie is a small piece of text that you can ask the user's browser to store for you so that you can read it back the next time the user visits your page.
You might think that you'd need to write the cookie information every time the map state changes, but in fact you only need to do it when the page exits. Instead of having the onunload="GUnload()" you can write onunload="setCookie()" and have your setCookie() function call GUnload() when it has finished.
A cookie can have an expiry time, after which the browser will delete the cookie. A browser with no expiry information is a "session cookie" and will disappear when the current browser session ends.
The cookie is stored by the user's browser. It is not stored on a server. Each user can have their own stored settings. If the user uses a different browser or a different computer, they will not get the same cookie.
In this example the page will remember the lat, lng, zoom and maptype for seven days.
Although the cookie code doesn't look too difficult, it seems to have a tendency to be awkward to debug. That's why I've put it in the "Advanced" section of the tutorial.