From 9f0a77aa92ee1224eb76114878d532ec6848b5d7 Mon Sep 17 00:00:00 2001 From: crschmidt Date: Sat, 1 Jul 2006 02:14:03 +0000 Subject: [PATCH] Add PermaLink control. Add test for permalink control. This control will automatically center and zoom the map to lat/lon/zoom args, and allows you to set an element whose href is modified when the map moves. controls.html implements an example. git-svn-id: http://svn.openlayers.org/trunk/openlayers@846 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- examples/controls.html | 4 ++- lib/OpenLayers.js | 1 + lib/OpenLayers/Control/Permalink.js | 56 +++++++++++++++++++++++++++++ tests/list-tests.html | 1 + tests/test_Control_Permalink.html | 47 ++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 lib/OpenLayers/Control/Permalink.js create mode 100644 tests/test_Control_Permalink.html diff --git a/examples/controls.html b/examples/controls.html index b2382cc2ca..0c4ea4d157 100644 --- a/examples/controls.html +++ b/examples/controls.html @@ -34,14 +34,16 @@ map.addControl(new OpenLayers.Control.PanZoomBar()); map.addControl(new OpenLayers.Control.MouseToolbar()); map.addControl(new OpenLayers.Control.LayerSwitcher()); + map.addControl(new OpenLayers.Control.Permalink($('permalink'))); // map.setCenter(new OpenLayers.LonLat(0, 0), 0); - map.zoomToMaxExtent(); + if (!map.getCenter()) map.zoomToMaxExtent(); } // -->

OpenLayers Example

+

diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 7e15db8125..54a02dfe54 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -84,6 +84,7 @@ if (typeof(_OPENLAYERS_SFL_) == "undefined") { "OpenLayers/Control/KeyboardDefaults.js", "OpenLayers/Control/PanZoom.js", "OpenLayers/Control/PanZoomBar.js", + "OpenLayers/Control/Permalink.js", "OpenLayers/Control/LayerSwitcher.js" ); // etc. diff --git a/lib/OpenLayers/Control/Permalink.js b/lib/OpenLayers/Control/Permalink.js new file mode 100644 index 0000000000..ab7798f6f7 --- /dev/null +++ b/lib/OpenLayers/Control/Permalink.js @@ -0,0 +1,56 @@ +/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license. + * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full + * text of the license. */ +// @require: OpenLayers/Control.js +OpenLayers.Control.Permalink = Class.create(); +OpenLayers.Control.Permalink.prototype = + Object.extend( new OpenLayers.Control(), { + + element: null, + + base: '', + + initialize: function(element, base) { + OpenLayers.Control.prototype.initialize.apply(this, arguments); + this.element = element; + if (base) this.base = base; + }, + + draw: function() { + this.map.events.register( 'moveend', this, this.updateLink); + var args = this.getArgs(); + if (args.lat && args.lon) { + this.map.setCenter( + new OpenLayers.LonLat(parseFloat(args.lon), parseFloat(args.lat)) + ); + } + if (args.zoom) { + this.map.zoomTo(parseInt(args.zoom)); + } + }, + + getArgs: function() { + var args = new Object(); + var query = location.search.substring(1); // Get query string. + var pairs = query.split("&"); // Break at ampersand. //+pjl + + for(var i = 0; i < pairs.length; i++) { + var pos = pairs[i].indexOf('='); // Look for "name=value". + if (pos == -1) continue; // If not found, skip. + var argname = pairs[i].substring(0,pos); // Extract the name. + var value = pairs[i].substring(pos+1); // Extract the value. + args[argname] = unescape(value); // Store as a property. + } + return args; // Return the object. + }, + + updateLink: function() { + center = this.map.getCenter(); + zoom = this.map.getZoom(); + lat = Math.round(center.lat*100000)/100000; + lon = Math.round(center.lon*100000)/100000; + this.element.href = this.base+"?lat="+lat+"&lon="+lon+"&zoom="+zoom; + }, + +}); + diff --git a/tests/list-tests.html b/tests/list-tests.html index b8033622d2..27133020b8 100644 --- a/tests/list-tests.html +++ b/tests/list-tests.html @@ -19,5 +19,6 @@
  • test_Control.html
  • test_Control_PanZoom.html
  • test_Control_PanZoomBar.html
  • +
  • test_Control_Permalink.html
  • test_Map.html
  • diff --git a/tests/test_Control_Permalink.html b/tests/test_Control_Permalink.html new file mode 100644 index 0000000000..5aebab512d --- /dev/null +++ b/tests/test_Control_Permalink.html @@ -0,0 +1,47 @@ + + + + + + +
    + Edit
    +
    + +