1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Control.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Class: OpenLayers.Control.ScaleLine |
---|
12 | * The ScaleLine displays a small line indicator representing the current |
---|
13 | * map scale on the map. By default it is drawn in the lower left corner of |
---|
14 | * the map. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Control> |
---|
18 | * |
---|
19 | * Is a very close copy of: |
---|
20 | * - <OpenLayers.Control.Scale> |
---|
21 | */ |
---|
22 | OpenLayers.Control.ScaleLine = OpenLayers.Class(OpenLayers.Control, { |
---|
23 | |
---|
24 | /** |
---|
25 | * Property: maxWidth |
---|
26 | * {Integer} Maximum width of the scale line in pixels. Default is 100. |
---|
27 | */ |
---|
28 | maxWidth: 100, |
---|
29 | |
---|
30 | /** |
---|
31 | * Property: topOutUnits |
---|
32 | * {String} Units for zoomed out on top bar. Default is km. |
---|
33 | */ |
---|
34 | topOutUnits: "km", |
---|
35 | |
---|
36 | /** |
---|
37 | * Property: topInUnits |
---|
38 | * {String} Units for zoomed in on top bar. Default is m. |
---|
39 | */ |
---|
40 | topInUnits: "m", |
---|
41 | |
---|
42 | /** |
---|
43 | * Property: bottomOutUnits |
---|
44 | * {String} Units for zoomed out on bottom bar. Default is mi. |
---|
45 | */ |
---|
46 | bottomOutUnits: "mi", |
---|
47 | |
---|
48 | /** |
---|
49 | * Property: bottomInUnits |
---|
50 | * {String} Units for zoomed in on bottom bar. Default is ft. |
---|
51 | */ |
---|
52 | bottomInUnits: "ft", |
---|
53 | |
---|
54 | /** |
---|
55 | * Property: eTop |
---|
56 | * {DOMElement} |
---|
57 | */ |
---|
58 | eTop: null, |
---|
59 | |
---|
60 | /** |
---|
61 | * Property: eBottom |
---|
62 | * {DOMElement} |
---|
63 | */ |
---|
64 | eBottom:null, |
---|
65 | |
---|
66 | /** |
---|
67 | * APIProperty: geodesic |
---|
68 | * {Boolean} Use geodesic measurement. Default is false. The recommended |
---|
69 | * setting for maps in EPSG:4326 is false, and true EPSG:900913. If set to |
---|
70 | * true, the scale will be calculated based on the horizontal size of the |
---|
71 | * pixel in the center of the map viewport. |
---|
72 | */ |
---|
73 | geodesic: false, |
---|
74 | |
---|
75 | /** |
---|
76 | * Constructor: OpenLayers.Control.ScaleLine |
---|
77 | * Create a new scale line control. |
---|
78 | * |
---|
79 | * Parameters: |
---|
80 | * options - {Object} An optional object whose properties will be used |
---|
81 | * to extend the control. |
---|
82 | */ |
---|
83 | initialize: function(options) { |
---|
84 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
---|
85 | }, |
---|
86 | |
---|
87 | /** |
---|
88 | * Method: draw |
---|
89 | * |
---|
90 | * Returns: |
---|
91 | * {DOMElement} |
---|
92 | */ |
---|
93 | draw: function() { |
---|
94 | OpenLayers.Control.prototype.draw.apply(this, arguments); |
---|
95 | if (!this.eTop) { |
---|
96 | // stick in the top bar |
---|
97 | this.eTop = document.createElement("div"); |
---|
98 | this.eTop.className = this.displayClass + "Top"; |
---|
99 | var theLen = this.topInUnits.length; |
---|
100 | this.div.appendChild(this.eTop); |
---|
101 | if((this.topOutUnits == "") || (this.topInUnits == "")) { |
---|
102 | this.eTop.style.visibility = "hidden"; |
---|
103 | } else { |
---|
104 | this.eTop.style.visibility = "visible"; |
---|
105 | } |
---|
106 | |
---|
107 | // and the bottom bar |
---|
108 | this.eBottom = document.createElement("div"); |
---|
109 | this.eBottom.className = this.displayClass + "Bottom"; |
---|
110 | this.div.appendChild(this.eBottom); |
---|
111 | if((this.bottomOutUnits == "") || (this.bottomInUnits == "")) { |
---|
112 | this.eBottom.style.visibility = "hidden"; |
---|
113 | } else { |
---|
114 | this.eBottom.style.visibility = "visible"; |
---|
115 | } |
---|
116 | } |
---|
117 | this.map.events.register('moveend', this, this.update); |
---|
118 | this.update(); |
---|
119 | return this.div; |
---|
120 | }, |
---|
121 | |
---|
122 | /** |
---|
123 | * Method: getBarLen |
---|
124 | * Given a number, round it down to the nearest 1,2,5 times a power of 10. |
---|
125 | * That seems a fairly useful set of number groups to use. |
---|
126 | * |
---|
127 | * Parameters: |
---|
128 | * maxLen - {float} the number we're rounding down from |
---|
129 | * |
---|
130 | * Returns: |
---|
131 | * {Float} the rounded number (less than or equal to maxLen) |
---|
132 | */ |
---|
133 | getBarLen: function(maxLen) { |
---|
134 | // nearest power of 10 lower than maxLen |
---|
135 | var digits = parseInt(Math.log(maxLen) / Math.log(10)); |
---|
136 | var pow10 = Math.pow(10, digits); |
---|
137 | |
---|
138 | // ok, find first character |
---|
139 | var firstChar = parseInt(maxLen / pow10); |
---|
140 | |
---|
141 | // right, put it into the correct bracket |
---|
142 | var barLen; |
---|
143 | if(firstChar > 5) { |
---|
144 | barLen = 5; |
---|
145 | } else if(firstChar > 2) { |
---|
146 | barLen = 2; |
---|
147 | } else { |
---|
148 | barLen = 1; |
---|
149 | } |
---|
150 | |
---|
151 | // scale it up the correct power of 10 |
---|
152 | return barLen * pow10; |
---|
153 | }, |
---|
154 | |
---|
155 | /** |
---|
156 | * Method: update |
---|
157 | * Update the size of the bars, and the labels they contain. |
---|
158 | */ |
---|
159 | update: function() { |
---|
160 | var res = this.map.getResolution(); |
---|
161 | if (!res) { |
---|
162 | return; |
---|
163 | } |
---|
164 | |
---|
165 | var curMapUnits = this.map.getUnits(); |
---|
166 | var inches = OpenLayers.INCHES_PER_UNIT; |
---|
167 | |
---|
168 | // convert maxWidth to map units |
---|
169 | var maxSizeData = this.maxWidth * res * inches[curMapUnits]; |
---|
170 | var geodesicRatio = 1; |
---|
171 | if(this.geodesic === true) { |
---|
172 | var maxSizeGeodesic = (this.map.getGeodesicPixelSize().w || |
---|
173 | 0.000001) * this.maxWidth; |
---|
174 | var maxSizeKilometers = maxSizeData / inches["km"]; |
---|
175 | geodesicRatio = maxSizeGeodesic / maxSizeKilometers; |
---|
176 | maxSizeData *= geodesicRatio; |
---|
177 | } |
---|
178 | |
---|
179 | // decide whether to use large or small scale units |
---|
180 | var topUnits; |
---|
181 | var bottomUnits; |
---|
182 | if(maxSizeData > 100000) { |
---|
183 | topUnits = this.topOutUnits; |
---|
184 | bottomUnits = this.bottomOutUnits; |
---|
185 | } else { |
---|
186 | topUnits = this.topInUnits; |
---|
187 | bottomUnits = this.bottomInUnits; |
---|
188 | } |
---|
189 | |
---|
190 | // and to map units units |
---|
191 | var topMax = maxSizeData / inches[topUnits]; |
---|
192 | var bottomMax = maxSizeData / inches[bottomUnits]; |
---|
193 | |
---|
194 | // now trim this down to useful block length |
---|
195 | var topRounded = this.getBarLen(topMax); |
---|
196 | var bottomRounded = this.getBarLen(bottomMax); |
---|
197 | |
---|
198 | // and back to display units |
---|
199 | topMax = topRounded / inches[curMapUnits] * inches[topUnits]; |
---|
200 | bottomMax = bottomRounded / inches[curMapUnits] * inches[bottomUnits]; |
---|
201 | |
---|
202 | // and to pixel units |
---|
203 | var topPx = topMax / res / geodesicRatio; |
---|
204 | var bottomPx = bottomMax / res / geodesicRatio; |
---|
205 | |
---|
206 | // now set the pixel widths |
---|
207 | // and the values inside them |
---|
208 | |
---|
209 | if (this.eBottom.style.visibility == "visible"){ |
---|
210 | this.eBottom.style.width = Math.round(bottomPx) + "px"; |
---|
211 | this.eBottom.innerHTML = bottomRounded + " " + bottomUnits ; |
---|
212 | } |
---|
213 | |
---|
214 | if (this.eTop.style.visibility == "visible"){ |
---|
215 | this.eTop.style.width = Math.round(topPx) + "px"; |
---|
216 | this.eTop.innerHTML = topRounded + " " + topUnits; |
---|
217 | } |
---|
218 | |
---|
219 | }, |
---|
220 | |
---|
221 | CLASS_NAME: "OpenLayers.Control.ScaleLine" |
---|
222 | }); |
---|
223 | |
---|