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 | /** |
---|
8 | * @requires OpenLayers/Control.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Control.MousePosition |
---|
13 | * The MousePosition control displays geographic coordinates of the mouse |
---|
14 | * pointer, as it is moved about the map. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Control> |
---|
18 | */ |
---|
19 | OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, { |
---|
20 | |
---|
21 | /** |
---|
22 | * APIProperty: autoActivate |
---|
23 | * {Boolean} Activate the control when it is added to a map. Default is |
---|
24 | * true. |
---|
25 | */ |
---|
26 | autoActivate: true, |
---|
27 | |
---|
28 | /** |
---|
29 | * Property: element |
---|
30 | * {DOMElement} |
---|
31 | */ |
---|
32 | element: null, |
---|
33 | |
---|
34 | /** |
---|
35 | * APIProperty: prefix |
---|
36 | * {String} |
---|
37 | */ |
---|
38 | prefix: '', |
---|
39 | |
---|
40 | /** |
---|
41 | * APIProperty: separator |
---|
42 | * {String} |
---|
43 | */ |
---|
44 | separator: ', ', |
---|
45 | |
---|
46 | /** |
---|
47 | * APIProperty: suffix |
---|
48 | * {String} |
---|
49 | */ |
---|
50 | suffix: '', |
---|
51 | |
---|
52 | /** |
---|
53 | * APIProperty: numDigits |
---|
54 | * {Integer} |
---|
55 | */ |
---|
56 | numDigits: 5, |
---|
57 | |
---|
58 | /** |
---|
59 | * APIProperty: granularity |
---|
60 | * {Integer} |
---|
61 | */ |
---|
62 | granularity: 10, |
---|
63 | |
---|
64 | /** |
---|
65 | * APIProperty: emptyString |
---|
66 | * {String} Set this to some value to set when the mouse is outside the |
---|
67 | * map. |
---|
68 | */ |
---|
69 | emptyString: null, |
---|
70 | |
---|
71 | /** |
---|
72 | * Property: lastXy |
---|
73 | * {<OpenLayers.Pixel>} |
---|
74 | */ |
---|
75 | lastXy: null, |
---|
76 | |
---|
77 | /** |
---|
78 | * APIProperty: displayProjection |
---|
79 | * {<OpenLayers.Projection>} The projection in which the |
---|
80 | * mouse position is displayed |
---|
81 | */ |
---|
82 | displayProjection: null, |
---|
83 | |
---|
84 | /** |
---|
85 | * Constructor: OpenLayers.Control.MousePosition |
---|
86 | * |
---|
87 | * Parameters: |
---|
88 | * options - {Object} Options for control. |
---|
89 | */ |
---|
90 | initialize: function(options) { |
---|
91 | OpenLayers.Control.prototype.initialize.apply(this, arguments); |
---|
92 | }, |
---|
93 | |
---|
94 | /** |
---|
95 | * Method: destroy |
---|
96 | */ |
---|
97 | destroy: function() { |
---|
98 | this.deactivate(); |
---|
99 | OpenLayers.Control.prototype.destroy.apply(this, arguments); |
---|
100 | }, |
---|
101 | |
---|
102 | /** |
---|
103 | * APIMethod: activate |
---|
104 | */ |
---|
105 | activate: function() { |
---|
106 | if (OpenLayers.Control.prototype.activate.apply(this, arguments)) { |
---|
107 | this.map.events.register('mousemove', this, this.redraw); |
---|
108 | this.map.events.register('mouseout', this, this.reset); |
---|
109 | this.redraw(); |
---|
110 | return true; |
---|
111 | } else { |
---|
112 | return false; |
---|
113 | } |
---|
114 | }, |
---|
115 | |
---|
116 | /** |
---|
117 | * APIMethod: deactivate |
---|
118 | */ |
---|
119 | deactivate: function() { |
---|
120 | if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { |
---|
121 | this.map.events.unregister('mousemove', this, this.redraw); |
---|
122 | this.map.events.unregister('mouseout', this, this.reset); |
---|
123 | this.element.innerHTML = ""; |
---|
124 | return true; |
---|
125 | } else { |
---|
126 | return false; |
---|
127 | } |
---|
128 | }, |
---|
129 | |
---|
130 | /** |
---|
131 | * Method: draw |
---|
132 | * {DOMElement} |
---|
133 | */ |
---|
134 | draw: function() { |
---|
135 | OpenLayers.Control.prototype.draw.apply(this, arguments); |
---|
136 | |
---|
137 | if (!this.element) { |
---|
138 | this.div.left = ""; |
---|
139 | this.div.top = ""; |
---|
140 | this.element = this.div; |
---|
141 | } |
---|
142 | |
---|
143 | return this.div; |
---|
144 | }, |
---|
145 | |
---|
146 | /** |
---|
147 | * Method: redraw |
---|
148 | */ |
---|
149 | redraw: function(evt) { |
---|
150 | |
---|
151 | var lonLat; |
---|
152 | |
---|
153 | if (evt == null) { |
---|
154 | this.reset(); |
---|
155 | return; |
---|
156 | } else { |
---|
157 | if (this.lastXy == null || |
---|
158 | Math.abs(evt.xy.x - this.lastXy.x) > this.granularity || |
---|
159 | Math.abs(evt.xy.y - this.lastXy.y) > this.granularity) |
---|
160 | { |
---|
161 | this.lastXy = evt.xy; |
---|
162 | return; |
---|
163 | } |
---|
164 | |
---|
165 | lonLat = this.map.getLonLatFromPixel(evt.xy); |
---|
166 | if (!lonLat) { |
---|
167 | // map has not yet been properly initialized |
---|
168 | return; |
---|
169 | } |
---|
170 | if (this.displayProjection) { |
---|
171 | lonLat.transform(this.map.getProjectionObject(), |
---|
172 | this.displayProjection ); |
---|
173 | } |
---|
174 | this.lastXy = evt.xy; |
---|
175 | |
---|
176 | } |
---|
177 | |
---|
178 | var newHtml = this.formatOutput(lonLat); |
---|
179 | |
---|
180 | if (newHtml != this.element.innerHTML) { |
---|
181 | this.element.innerHTML = newHtml; |
---|
182 | } |
---|
183 | }, |
---|
184 | |
---|
185 | /** |
---|
186 | * Method: reset |
---|
187 | */ |
---|
188 | reset: function(evt) { |
---|
189 | if (this.emptyString != null) { |
---|
190 | this.element.innerHTML = this.emptyString; |
---|
191 | } |
---|
192 | }, |
---|
193 | |
---|
194 | /** |
---|
195 | * Method: formatOutput |
---|
196 | * Override to provide custom display output |
---|
197 | * |
---|
198 | * Parameters: |
---|
199 | * lonLat - {<OpenLayers.LonLat>} Location to display |
---|
200 | */ |
---|
201 | formatOutput: function(lonLat) { |
---|
202 | var digits = parseInt(this.numDigits); |
---|
203 | var newHtml = |
---|
204 | this.prefix + |
---|
205 | lonLat.lon.toFixed(digits) + |
---|
206 | this.separator + |
---|
207 | lonLat.lat.toFixed(digits) + |
---|
208 | this.suffix; |
---|
209 | return newHtml; |
---|
210 | }, |
---|
211 | |
---|
212 | CLASS_NAME: "OpenLayers.Control.MousePosition" |
---|
213 | }); |
---|