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

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