Files
openlayers/lib/OpenLayers/Control/Permalink.js
crschmidt 97418cb70b Permalink will now save state if there's no base layer when it's added, and
change the map when the first baselayer loads.


git-svn-id: http://svn.openlayers.org/trunk/openlayers@1537 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2006-10-03 04:34:51 +00:00

98 lines
2.9 KiB
JavaScript

/* 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. */
/**
* @class
*
* @requires OpenLayers/Control.js
*/
OpenLayers.Control.Permalink = Class.create();
OpenLayers.Control.Permalink.prototype =
Object.extend( new OpenLayers.Control(), {
/** @type DOMElement */
element: null,
/** @type String */
base: '',
/**
* @constructor
*
* @param {DOMElement} element
* @param {String} base
*/
initialize: function(element, base) {
OpenLayers.Control.prototype.initialize.apply(this, arguments);
this.element = element;
if (base) this.base = base;
},
/**
* @type DOMElement
*/
draw: function() {
OpenLayers.Control.prototype.draw.apply(this, arguments);
var args = OpenLayers.Util.getArgs();
if (args.lat && args.lon) {
if (this.map.baseLayer) {
this.map.setCenter(
new OpenLayers.LonLat(parseFloat(args.lon), parseFloat(args.lat))
);
} else {
this.centerData = new OpenLayers.LonLat(
parseFloat(args.lon),
parseFloat(args.lat));
this.map.events.register( 'changebaselayer', this, this.setCenter);
}
}
if (args.zoom) {
if (this.map.baseLayer) {
this.map.zoomTo(parseInt(args.zoom));
} else {
this.zoomData = parseInt(args.zoom);
}
}
if (!this.element) {
this.element = document.createElement("a");
this.div.style.right = "3px";
this.div.style.bottom = "3px";
this.div.style.left = "";
this.div.style.top = "";
this.div.style.display = "block";
this.div.style.position = "absolute";
this.element.style.fontSize="smaller";
this.element.innerHTML = "Permalink";
this.element.href="";
this.div.appendChild(this.element);
}
this.map.events.register( 'moveend', this, this.updateLink);
return this.div;
},
/**
*
*/
updateLink: function() {
var center = this.map.getCenter();
var zoom = this.map.getZoom();
var lat = Math.round(center.lat*100000)/100000;
var lon = Math.round(center.lon*100000)/100000;
this.element.href = this.base+"?lat="+lat+"&lon="+lon+"&zoom="+zoom;
},
setCenter: function() {
if (this.map.baseLayer && this.centerData) {
this.map.setCenter(this.centerData, this.zoomData ? this.zoomData : null);
this.centerData = null;
}
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Control.Permalink"
});