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 <a> 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
This commit is contained in:
crschmidt
2006-07-01 02:14:03 +00:00
parent 72e44d6638
commit 9f0a77aa92
5 changed files with 108 additions and 1 deletions

View File

@@ -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();
}
// -->
</script>
</head>
<body onload="init()">
<h1>OpenLayers Example</h1>
<a href="" id="permalink">Permalink</a><br />
<div id="map"></div>
</body>
</html>

View File

@@ -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.

View File

@@ -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;
},
});

View File

@@ -19,5 +19,6 @@
<li>test_Control.html</li>
<li>test_Control_PanZoom.html</li>
<li>test_Control_PanZoomBar.html</li>
<li>test_Control_Permalink.html</li>
<li>test_Map.html</li>
</ul>

View File

@@ -0,0 +1,47 @@
<html>
<head>
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript"><!--
var map;
function test_01_Control_Permalink_constructor (t) {
t.plan( 1 );
control = new OpenLayers.Control.Permalink();
t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
}
function test_02_Control_Permalink_updateLinks (t) {
t.plan( 2 );
control = new OpenLayers.Control.Permalink($('permalink'));
t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
map = new OpenLayers.Map($('map'));
layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
map.addLayer(layer);
if (!map.getCenter()) map.zoomToMaxExtent();
map.addControl(control);
map.pan(5, 0);
t.eq($('permalink').href, location+"?lat=0&lon=1.75781&zoom=2", "Panning sets permalink");
}
function test_03_Control_Permalink_updateLinksBase (t) {
t.plan( 2 );
control = new OpenLayers.Control.Permalink($('permalink'), "./edit.html" );
t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" );
map = new OpenLayers.Map($('map'));
layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
map.addLayer(layer);
if (!map.getCenter()) map.zoomToMaxExtent();
map.addControl(control);
map.pan(5, 0);
$('edit_permalink').href = './edit.html?lat=0&lon=1.75781&zoom=2';
t.eq($('permalink').href, $('edit_permalink').href, "Panning sets permalink with base");
}
// -->
</script>
</head>
<body>
<a id="permalink" href="">Permalink</a> <br />
<a id="edit_permalink" href="">Edit</a> <br />
<div id="map" style="width: 1024px; height: 512px;"/>
</body>
</html>