]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Control/MousePosition.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Control / MousePosition.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.MousePosition
12  * The MousePosition control displays geographic coordinates of the mouse
13  * pointer, as it is moved about the map.
14  *
15  * Inherits from:
16  *  - <OpenLayers.Control>
17  */
18 OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, {
19     
20     /** 
21      * Property: element
22      * {DOMElement} 
23      */
24     element: null,
25     
26     /** 
27      * APIProperty: prefix
28      * {String}
29      */
30     prefix: '',
31     
32     /** 
33      * APIProperty: separator
34      * {String}
35      */
36     separator: ', ',
37     
38     /** 
39      * APIProperty: suffix
40      * {String}
41      */
42     suffix: '',
43     
44     /** 
45      * APIProperty: numDigits
46      * {Integer}
47      */
48     numDigits: 5,
49     
50     /** 
51      * APIProperty: granularity
52      * {Integer} 
53      */
54     granularity: 10,
55     
56     /** 
57      * Property: lastXy
58      * {<OpenLayers.Pixel>}
59      */
60     lastXy: null,
61
62     /**
63      * APIProperty: displayProjection
64      * {<OpenLayers.Projection>} The projection in which the 
65      * mouse position is displayed
66      */
67     displayProjection: null, 
68     
69     /**
70      * Constructor: OpenLayers.Control.MousePosition
71      * 
72      * Parameters:
73      * options - {Object} Options for control.
74      */
75     initialize: function(options) {
76         OpenLayers.Control.prototype.initialize.apply(this, arguments);
77     },
78
79     /**
80      * Method: destroy
81      */
82      destroy: function() {
83          if (this.map) {
84              this.map.events.unregister('mousemove', this, this.redraw);
85          }
86          OpenLayers.Control.prototype.destroy.apply(this, arguments);
87      },
88
89     /**
90      * Method: draw
91      * {DOMElement}
92      */    
93     draw: function() {
94         OpenLayers.Control.prototype.draw.apply(this, arguments);
95
96         if (!this.element) {
97             this.div.left = "";
98             this.div.top = "";
99             this.element = this.div;
100         }
101         
102         this.redraw();
103         return this.div;
104     },
105    
106     /**
107      * Method: redraw  
108      */
109     redraw: function(evt) {
110
111         var lonLat;
112
113         if (evt == null) {
114             lonLat = new OpenLayers.LonLat(0, 0);
115         } else {
116             if (this.lastXy == null ||
117                 Math.abs(evt.xy.x - this.lastXy.x) > this.granularity ||
118                 Math.abs(evt.xy.y - this.lastXy.y) > this.granularity)
119             {
120                 this.lastXy = evt.xy;
121                 return;
122             }
123
124             lonLat = this.map.getLonLatFromPixel(evt.xy);
125             if (!lonLat) { 
126                 // map has not yet been properly initialized
127                 return;
128             }    
129             if (this.displayProjection) {
130                 lonLat.transform(this.map.getProjectionObject(), 
131                                  this.displayProjection );
132             }      
133             this.lastXy = evt.xy;
134             
135         }
136         
137         var newHtml = this.formatOutput(lonLat);
138
139         if (newHtml != this.element.innerHTML) {
140             this.element.innerHTML = newHtml;
141         }
142     },
143
144     /**
145      * Method: formatOutput
146      * Override to provide custom display output
147      *
148      * Parameters:
149      * lonLat - {<OpenLayers.LonLat>} Location to display
150      */
151     formatOutput: function(lonLat) {
152         var digits = parseInt(this.numDigits);
153         var newHtml =
154             this.prefix +
155             lonLat.lon.toFixed(digits) +
156             this.separator + 
157             lonLat.lat.toFixed(digits) +
158             this.suffix;
159         return newHtml;
160      },
161
162     /** 
163      * Method: setMap
164      */
165     setMap: function() {
166         OpenLayers.Control.prototype.setMap.apply(this, arguments);
167         this.map.events.register( 'mousemove', this, this.redraw);
168     },     
169
170     CLASS_NAME: "OpenLayers.Control.MousePosition"
171 });