MousePosition.js 4.8 KB

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