]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Control/Scale.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Control / Scale.js
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. */
4
5
6 /**
7  * @requires OpenLayers/Control.js
8  */
9
10 /**
11  * Class: OpenLayers.Control.Scale
12  * The Scale control displays the current map scale as a ratio (e.g. Scale = 
13  * 1:1M). By default it is displayed in the lower right corner of the map.
14  *
15  * Inherits from:
16  *  - <OpenLayers.Control>
17  */
18 OpenLayers.Control.Scale = OpenLayers.Class(OpenLayers.Control, {
19     
20     /**
21      * Parameter: element
22      * {DOMElement}
23      */
24     element: null,
25     
26     /**
27      * Constructor: OpenLayers.Control.Scale
28      * 
29      * Parameters:
30      * element - {DOMElement} 
31      * options - {Object} 
32      */
33     initialize: function(element, options) {
34         OpenLayers.Control.prototype.initialize.apply(this, [options]);
35         this.element = OpenLayers.Util.getElement(element);        
36     },
37
38     /**
39      * Method: draw
40      * 
41      * Returns:
42      * {DOMElement}
43      */    
44     draw: function() {
45         OpenLayers.Control.prototype.draw.apply(this, arguments);
46         if (!this.element) {
47             this.element = document.createElement("div");
48             this.div.appendChild(this.element);
49         }
50         this.map.events.register( 'moveend', this, this.updateScale);
51         this.updateScale();
52         return this.div;
53     },
54    
55     /**
56      * Method: updateScale
57      */
58     updateScale: function() {
59         var scale = this.map.getScale();
60         if (!scale) {
61             return;
62         }
63
64         if (scale >= 9500 && scale <= 950000) {
65             scale = Math.round(scale / 1000) + "K";
66         } else if (scale >= 950000) {
67             scale = Math.round(scale / 1000000) + "M";
68         } else {
69             scale = Math.round(scale);
70         }    
71         
72         this.element.innerHTML = OpenLayers.i18n("scale", {'scaleDenom':scale});
73     }, 
74
75     CLASS_NAME: "OpenLayers.Control.Scale"
76 });
77