1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
7 * @requires OpenLayers/Control.js
8 * @requires OpenLayers/Control/ArgParser.js
12 * Class: OpenLayers.Control.Permalink
13 * The Permalink control is hyperlink that will return the user to the
14 * current map view. By default it is drawn in the lower right corner of the
15 * map. The href is updated as the map is zoomed, panned and whilst layers
19 * - <OpenLayers.Control>
21 OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
24 * APIProperty: argParserClass
25 * {Class} The ArgParser control class (not instance) to use with this
28 argParserClass: OpenLayers.Control.ArgParser,
43 * APIProperty: displayProjection
44 * {<OpenLayers.Projection>} Requires proj4js support. Projection used
45 * when creating the coordinates in the link. This will reproject the
46 * map coordinates into display coordinates. If you are using this
47 * functionality, the permalink which is last added to the map will
48 * determine the coordinate type which is read from the URL, which
49 * means you should not add permalinks with different
50 * displayProjections to the same map.
52 displayProjection: null,
55 * Constructor: OpenLayers.Control.Permalink
58 * element - {DOMElement}
60 * options - {Object} options to the control.
62 initialize: function(element, base, options) {
63 OpenLayers.Control.prototype.initialize.apply(this, [options]);
64 this.element = OpenLayers.Util.getElement(element);
65 this.base = base || document.location.href;
72 if (this.element.parentNode == this.div) {
73 this.div.removeChild(this.element);
77 this.map.events.unregister('moveend', this, this.updateLink);
79 OpenLayers.Control.prototype.destroy.apply(this, arguments);
84 * Set the map property for the control.
87 * map - {<OpenLayers.Map>}
89 setMap: function(map) {
90 OpenLayers.Control.prototype.setMap.apply(this, arguments);
92 //make sure we have an arg parser attached
93 for(var i=0, len=this.map.controls.length; i<len; i++) {
94 var control = this.map.controls[i];
95 if (control.CLASS_NAME == this.argParserClass.CLASS_NAME) {
97 // If a permalink is added to the map, and an ArgParser already
98 // exists, we override the displayProjection to be the one
100 if (control.displayProjection != this.displayProjection) {
101 this.displayProjection = control.displayProjection;
107 if (i == this.map.controls.length) {
108 this.map.addControl(new this.argParserClass(
109 { 'displayProjection': this.displayProjection }));
121 OpenLayers.Control.prototype.draw.apply(this, arguments);
124 this.div.className = this.displayClass;
125 this.element = document.createElement("a");
126 this.element.innerHTML = OpenLayers.i18n("permalink");
127 this.element.href="";
128 this.div.appendChild(this.element);
131 'moveend': this.updateLink,
132 'changelayer': this.updateLink,
133 'changebaselayer': this.updateLink,
137 // Make it so there is at least a link even though the map may not have
147 updateLink: function() {
148 var href = this.base;
149 if (href.indexOf('?') != -1) {
150 href = href.substring( 0, href.indexOf('?') );
153 href += '?' + OpenLayers.Util.getParameterString(this.createParams());
154 this.element.href = href;
158 * APIMethod: createParams
159 * Creates the parameters that need to be encoded into the permalink url.
162 * center - {<OpenLayers.LonLat>} center to encode in the permalink.
163 * Defaults to the current map center.
164 * zoom - {Integer} zoom level to encode in the permalink. Defaults to the
165 * current map zoom level.
166 * layers - {Array(<OpenLayers.Layer>)} layers to encode in the permalink.
167 * Defaults to the current map layers.
170 * {Object} Hash of parameters that will be url-encoded into the
173 createParams: function(center, zoom, layers) {
174 center = center || this.map.getCenter();
176 var params = OpenLayers.Util.getParameters(this.base);
178 // If there's still no center, map is not initialized yet.
179 // Break out of this function, and simply return the params from the
184 params.zoom = zoom || this.map.getZoom();
187 var lat = center.lat;
188 var lon = center.lon;
190 if (this.displayProjection) {
191 var mapPosition = OpenLayers.Projection.transform(
193 this.map.getProjectionObject(),
194 this.displayProjection );
198 params.lat = Math.round(lat*100000)/100000;
199 params.lon = Math.round(lon*100000)/100000;
202 layers = layers || this.map.layers;
204 for (var i=0, len=layers.length; i<len; i++) {
205 var layer = layers[i];
207 if (layer.isBaseLayer) {
208 params.layers += (layer == this.map.baseLayer) ? "B" : "0";
210 params.layers += (layer.getVisibility()) ? "T" : "F";
218 CLASS_NAME: "OpenLayers.Control.Permalink"