Logic, but in a different way

Daft Logic

Google Maps Remember Last location

Last Updated 16th December 2008
Using GMap2.savePosition() and GMap2.returnToSavedPosition()
 
Using cookies via Javascript
 

Description

A demonstration of two methods for saving and reloading a position on a Google Map. This allows a preset location to be brought up quickly without the user spending time remembering or panning and zooming to the position. A position is assumed to be a combination all all the following:

Using GMap2.savePosition() and GMap2.returnToSavedPosition()

How To Use

  1. Once the map has fully loaded on the page, pan and zoom to find a particular location
  2. Click Save
  3. Move the map to somewhere else
  4. Click Load
  5. The map should return to its saved state

How it Works

The methods GMap2.savePosition() and GMap2.returnToSavedPosition() are included in the Google Maps API and are very easy to use in the required events.

The code use to call these methods is...

function Load() { map.returnToSavedPosition(); } function Save() { map.savePosition(); }

Advantages and Disadvantages

Using Cookies

How To Use

  1. Once the map has fully loaded on the page, pan and zoom to find a particular location
  2. Click Save
  3. Move the map to somewhere else
  4. Click Load
  5. The map should return to its saved state
  6. Then try navigating away from this page and then try to load again

How it Works

The code use save and load the location is...

function Save() { var mapzoom=map.getZoom(); var mapcenter=map.getCenter(); var maplat=mapcenter.lat(); var maplng=mapcenter.lng(); var cookiestring=maplat+"_"+maplng+"_"+mapzoom; var exp = new Date(); //set new date object exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30)); //set it 30 days ahead setCookie("DaftLogicGMRLL",cookiestring, exp); } function Load() { var loadedstring=getCookie("DaftLogicGMRLL"); var splitstr = loadedstring.split("_"); map.setCenter(new GLatLng(parseFloat(splitstr[0]), parseFloat(splitstr[1])), parseFloat(splitstr[2])); } function setCookie(name, value, expires) { document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString()); } function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "="); if (c_start!=-1) { c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; return unescape(document.cookie.substring(c_start,c_end)); } } return ""; }

Advantages and Disadvantages

Version History

Previous Comments For This Page

Hi Would it be that hard to enable the user to save multiple maps and load them at another vist
By Lee on 29/06/2009
Hi there, thanks for your help earlier. I have 2 questions 1. When the page loads the map will only work properly if you with the reset icon, otherwise the drag and zoom options seem really sensitive. Any ideas. Page can be found here http://www.vineyardroads.co.nz/cookieresults.php?id=1&tastings=0&Submit=Search 2. On this site I have one map page used for 17 regions, if I use your code, it remembers the location for 1 map and if you choose another region it loads up the previous maps location. How do I save multiple cookies for each location (or save multiple sets of data in the same cookie?) Thanks in advance Steven
By Steven on 29/06/2009
Try using the event http://code.google.com/apis/maps/documentation/reference.html#GMap2.moveend to call the Save function.
By Daft Logic on 13/02/2009
Hi there, great work with maps, I am wanting to do the same, but dont want to have the user clicking save buttons. Is there a way for the cookie to be updated verytime the maps is zoomed or moved? So if the user navigates away, they can come back to the same place Cheers
By Steven on 11/02/2009

Add your own comment below and let others know what you think:

Comments :

Your Name (optional) :