| 44 | | if(!restoreState()) { |
|---|
| 45 | | |
|---|
| 46 | | // create Google Mercator layers |
|---|
| 47 | | var gmap = new OpenLayers.Layer.Google( |
|---|
| 48 | | "Google Streets", |
|---|
| 49 | | {'sphericalMercator': true} |
|---|
| 50 | | ); |
|---|
| 51 | | |
|---|
| 52 | | // create WMS layer |
|---|
| 53 | | var wms = new OpenLayers.Layer.WMS( |
|---|
| 54 | | "World Map", |
|---|
| 55 | | "http://world.freemap.in/tiles/", |
|---|
| 56 | | {'layers': 'factbook-overlay', 'format':'png'}, |
|---|
| 57 | | { |
|---|
| 58 | | 'reproject': false, 'opacity': 0.4, |
|---|
| 59 | | 'isBaseLayer': false,'wrapDateLine': true |
|---|
| 60 | | } |
|---|
| 61 | | ); |
|---|
| 62 | | |
|---|
| 63 | | // create a vector layer for drawing |
|---|
| 64 | | var vector = new OpenLayers.Layer.Vector("Editable Vectors"); |
|---|
| 65 | | |
|---|
| 66 | | map.addLayers([gmap, wms, vector]); |
|---|
| 67 | | panel = new OpenLayers.Control.EditingToolbar(vector); |
|---|
| 68 | | map.addControl(panel); |
|---|
| 69 | | switcher = new OpenLayers.Control.LayerSwitcher(); |
|---|
| 70 | | map.addControl(switcher); |
|---|
| 71 | | map.zoomToMaxExtent(); |
|---|
| 72 | | } |
|---|
| | 32 | // create Google Mercator layers |
|---|
| | 33 | var gmap = new OpenLayers.Layer.Google( |
|---|
| | 34 | "Google Streets", |
|---|
| | 35 | {'sphericalMercator': true} |
|---|
| | 36 | ); |
|---|
| | 38 | // create WMS layer |
|---|
| | 39 | var wms = new OpenLayers.Layer.WMS( |
|---|
| | 40 | "World Map", |
|---|
| | 41 | "http://world.freemap.in/tiles/", |
|---|
| | 42 | {'layers': 'factbook-overlay', 'format':'png'}, |
|---|
| | 43 | { |
|---|
| | 44 | 'reproject': false, 'opacity': 0.4, |
|---|
| | 45 | 'isBaseLayer': false,'wrapDateLine': true |
|---|
| | 46 | } |
|---|
| | 47 | ); |
|---|
| | 48 | |
|---|
| | 49 | // create a vector layer for drawing |
|---|
| | 50 | var vector = new OpenLayers.Layer.Vector("Editable Vectors"); |
|---|
| | 51 | |
|---|
| | 52 | map.addLayers([gmap, wms, vector]); |
|---|
| | 53 | panel = new OpenLayers.Control.EditingToolbar(vector); |
|---|
| | 54 | // this should be in EditingToolbar |
|---|
| | 55 | panel.defaultControl = panel.controls[0]; |
|---|
| | 56 | map.addControl(panel); |
|---|
| | 57 | switcher = new OpenLayers.Control.LayerSwitcher(); |
|---|
| | 58 | map.addControl(switcher); |
|---|
| | 59 | map.zoomToMaxExtent(); |
|---|
| 79 | | var str = olon.write(map); |
|---|
| 80 | | if(str.length < 3950) { |
|---|
| 81 | | createCookie("olmap", str, 10); |
|---|
| 82 | | document.getElementById('output').value = "saved:\n\n" + str; |
|---|
| 83 | | } else { |
|---|
| 84 | | document.getElementById('output').value = "too long:\n\n" + str; |
|---|
| 85 | | } |
|---|
| | 65 | panel.deactivate(); |
|---|
| | 66 | store.set("olon", olon.write(map), function(ok, str) { |
|---|
| | 67 | if(ok) { |
|---|
| | 68 | report("saved:\n\n" + str); |
|---|
| | 69 | } else { |
|---|
| | 70 | report("failed to save map"); |
|---|
| | 71 | } |
|---|
| | 72 | }); |
|---|
| | 73 | panel.activate(); |
|---|
| 87 | | function restoreState() { |
|---|
| 88 | | var restored = false; |
|---|
| 89 | | var str = readCookie("olmap"); |
|---|
| 90 | | if(str) { |
|---|
| 91 | | deserializeMap(str); |
|---|
| 92 | | restored = true; |
|---|
| 93 | | document.getElementById('output').value = "restored:\n\n" + str; |
|---|
| 94 | | } else { |
|---|
| 95 | | document.getElementById('output').value = 'no state saved'; |
|---|
| 96 | | } |
|---|
| 97 | | return restored; |
|---|
| | 75 | function restoreState(failure) { |
|---|
| | 76 | store.get("olon", function(ok, str) { |
|---|
| | 77 | if(ok && str) { |
|---|
| | 78 | if(typeof str == "object") { |
|---|
| | 79 | str = str.value; |
|---|
| | 80 | } |
|---|
| | 81 | deserializeMap(str); |
|---|
| | 82 | report("restored:\n\n" + str); |
|---|
| | 83 | } else { |
|---|
| | 84 | report("no state saved"); |
|---|
| | 85 | if(failure) { |
|---|
| | 86 | failure(); |
|---|
| | 87 | } |
|---|
| | 88 | } |
|---|
| | 89 | }); |
|---|
| 99 | | |
|---|
| 100 | | function createCookie(name, value, days) { |
|---|
| 101 | | var expires = ""; |
|---|
| 102 | | if(days) { |
|---|
| 103 | | var date = new Date(); |
|---|
| 104 | | date.setTime(date.getTime()+(days*24*60*60*1000)); |
|---|
| 105 | | expires = "; expires="+date.toGMTString(); |
|---|
| 106 | | } |
|---|
| 107 | | document.cookie = name + "=" + value + expires + "; path=/"; |
|---|
| 108 | | } |
|---|
| 109 | | function readCookie(name) { |
|---|
| 110 | | var value = null; |
|---|
| 111 | | var nameEQ = name + "="; |
|---|
| 112 | | var ca = document.cookie.split(';'); |
|---|
| 113 | | for(var i=0; i<ca.length; i++) { |
|---|
| 114 | | var c = ca[i].replace(/^s+/, ''); |
|---|
| 115 | | if(c.indexOf(nameEQ) == 0) { |
|---|
| 116 | | value = c.substring(nameEQ.length, c.length); |
|---|
| 117 | | break; |
|---|
| 118 | | } |
|---|
| 119 | | } |
|---|
| 120 | | return value; |
|---|
| 121 | | } |
|---|
| 122 | | function eraseCookie(name) { |
|---|
| 123 | | createCookie(name,"",-1); |
|---|
| | 91 | |
|---|
| | 92 | function report(msg) { |
|---|
| | 93 | document.getElementById('output').value = msg; |
|---|
| 182 | | <p>This example show how a map can be serialized using OpenLayers Object Notation. |
|---|
| 183 | | The serialized map can be saved somewhere (like a cookie if it's under 4KB), and |
|---|
| 184 | | deserialized to restore the state of a map. Serializing features on a vector layer |
|---|
| 185 | | is possible, but creates a string that is typically too large for cookie storage</p> |
|---|
| 186 | | <p>Navigate to some location, turn layers on/off, click save state. Close this page |
|---|
| 187 | | and reopen it (or just refresh) to see the state restored.</p> |
|---|
| | 157 | <div id="docs"> |
|---|
| | 158 | <p>This example show how a map can be serialized using OpenLayers |
|---|
| | 159 | Object Notation. The serialized map can be saved somewhere, and |
|---|
| | 160 | deserialized to restore the state of a map.</p> |
|---|
| | 161 | <p>Navigate to some location, turn layers on/off, click save state. |
|---|
| | 162 | Close this page and reopen it (or just refresh) to see the state |
|---|
| | 163 | restored.</p> |
|---|
| | 164 | </div> |
|---|