[OpenLayers-Users] saving a state of a map

Greg Allensworth gregor at greeninfo.org
Fri Jul 15 14:02:22 EDT 2011


On 7/15/2011 10:56 AM, Michael wrote:
> I have been using cookies. Set a cookie with each change, read when the
> page is invoked. Have not been using for pan and zoom, but should be the
> same...

Agreed; we have a map where we set cookies to save the map's state: the 
zoom and X/Y location, etc. Here's even some sample code to get you started.

This basic cookie library for JavaScript,
http://agatlas.mapsportal.org/media/js/cookie.js
taken from this site originally:
http://techpatterns.com/downloads/javascript_cookies.php


Throw a few event handlers onto your map, to save the state to cookies:

map.events.register('moveend', map, function () {
    Set_Cookie('mapx', map.getCenter().lon );
    Set_Cookie('mapy', map.getCenter().lat );
    Set_Cookie('mapz', map.getZoom() );
});
map.events.register('changebaselayer', map, function () {
    Set_Cookie('base', map.baseLayer.name);
});



And recall the state from cookies:

   var lon = Get_Cookie('mapx');
   var lat = Get_Cookie('mapy');
   var zum = Get_Cookie('mapz');
   if (lon && lat && zum) {
     map.setCenter(new OpenLayers.LonLat(lon,lat),zum);
   }

   var base = Get_Cookie('base');
   if (base) selectBasemap(base);


-- 
Greg Allensworth
Web GIS Developer, GreenInfo Network
BS  A+  Network+  Security+  Linux+  Server+


More information about the Users mailing list