Files
openlayers/lib/OpenLayers/Control/Scale.js
crschmidt efb925d632 Commit fixes for:
* (Closes #2360) make Layer.addOptions call initResolutions if necessary
 * (Closes #2656) no way to pass read options from protocol to format
 * (Closes #2751) Changes in languages: "es" and "ca".
 * (Closes #2814) SLDSelect control tests failing
 * (Closes #2815) Cluster Strategy should not destroy all features

Also:
 * Change examples to use OSGeo, rather than MetaCarta, servers.
 * Change copyright statements to correctly state joint copyright
   in the project, rather than MetaCarta copyright.


git-svn-id: http://svn.openlayers.org/branches/openlayers/2.10@10715 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2010-09-02 21:43:25 +00:00

100 lines
2.7 KiB
JavaScript

/* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Control.js
*/
/**
* Class: OpenLayers.Control.Scale
* The Scale control displays the current map scale as a ratio (e.g. Scale =
* 1:1M). By default it is displayed in the lower right corner of the map.
*
* Inherits from:
* - <OpenLayers.Control>
*/
OpenLayers.Control.Scale = OpenLayers.Class(OpenLayers.Control, {
/**
* Parameter: element
* {DOMElement}
*/
element: null,
/**
* APIProperty: geodesic
* {Boolean} Use geodesic measurement. Default is false. The recommended
* setting for maps in EPSG:4326 is false, and true EPSG:900913. If set to
* true, the scale will be calculated based on the horizontal size of the
* pixel in the center of the map viewport.
*/
geodesic: false,
/**
* Constructor: OpenLayers.Control.Scale
*
* Parameters:
* element - {DOMElement}
* options - {Object}
*/
initialize: function(element, options) {
OpenLayers.Control.prototype.initialize.apply(this, [options]);
this.element = OpenLayers.Util.getElement(element);
},
/**
* Method: draw
*
* Returns:
* {DOMElement}
*/
draw: function() {
OpenLayers.Control.prototype.draw.apply(this, arguments);
if (!this.element) {
this.element = document.createElement("div");
this.div.appendChild(this.element);
}
this.map.events.register( 'moveend', this, this.updateScale);
this.updateScale();
return this.div;
},
/**
* Method: updateScale
*/
updateScale: function() {
var scale;
if(this.geodesic === true) {
var units = this.map.getUnits();
if(!units) {
return;
}
var inches = OpenLayers.INCHES_PER_UNIT;
scale = (this.map.getGeodesicPixelSize().w || 0.000001) *
inches["km"] * OpenLayers.DOTS_PER_INCH;
} else {
scale = this.map.getScale();
}
if (!scale) {
return;
}
if (scale >= 9500 && scale <= 950000) {
scale = Math.round(scale / 1000) + "K";
} else if (scale >= 950000) {
scale = Math.round(scale / 1000000) + "M";
} else {
scale = Math.round(scale);
}
this.element.innerHTML = OpenLayers.i18n("scale", {'scaleDenom':scale});
},
CLASS_NAME: "OpenLayers.Control.Scale"
});