MousePosition.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository 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. * You can use the <prefix>- or <suffix>-properties to provide more information
  14. * about the displayed coordinates to the user:
  15. *
  16. * (code)
  17. * var mousePositionCtrl = new OpenLayers.Control.MousePosition({
  18. * prefix: '<a target="_blank" ' +
  19. * 'href="http://spatialreference.org/ref/epsg/4326/">' +
  20. * 'EPSG:4326</a> coordinates: '
  21. * }
  22. * );
  23. * (end code)
  24. *
  25. * Inherits from:
  26. * - <OpenLayers.Control>
  27. */
  28. OpenLayers.Control.MousePosition = OpenLayers.Class(OpenLayers.Control, {
  29. /**
  30. * APIProperty: autoActivate
  31. * {Boolean} Activate the control when it is added to a map. Default is
  32. * true.
  33. */
  34. autoActivate: true,
  35. /**
  36. * Property: element
  37. * {DOMElement}
  38. */
  39. element: null,
  40. /**
  41. * APIProperty: prefix
  42. * {String} A string to be prepended to the current pointers coordinates
  43. * when it is rendered. Defaults to the empty string ''.
  44. */
  45. prefix: '',
  46. /**
  47. * APIProperty: separator
  48. * {String} A string to be used to seperate the two coordinates from each
  49. * other. Defaults to the string ', ', which will result in a
  50. * rendered coordinate of e.g. '42.12, 21.22'.
  51. */
  52. separator: ', ',
  53. /**
  54. * APIProperty: suffix
  55. * {String} A string to be appended to the current pointers coordinates
  56. * when it is rendered. Defaults to the empty string ''.
  57. */
  58. suffix: '',
  59. /**
  60. * APIProperty: numDigits
  61. * {Integer} The number of digits each coordinate shall have when being
  62. * rendered, Defaults to 5.
  63. */
  64. numDigits: 5,
  65. /**
  66. * APIProperty: granularity
  67. * {Integer}
  68. */
  69. granularity: 10,
  70. /**
  71. * APIProperty: emptyString
  72. * {String} Set this to some value to set when the mouse is outside the
  73. * map.
  74. */
  75. emptyString: null,
  76. /**
  77. * Property: lastXy
  78. * {<OpenLayers.Pixel>}
  79. */
  80. lastXy: null,
  81. /**
  82. * APIProperty: displayProjection
  83. * {<OpenLayers.Projection>} The projection in which the mouse position is
  84. * displayed.
  85. */
  86. displayProjection: null,
  87. /**
  88. * Constructor: OpenLayers.Control.MousePosition
  89. *
  90. * Parameters:
  91. * options - {Object} Options for control.
  92. */
  93. /**
  94. * Method: destroy
  95. */
  96. destroy: function() {
  97. this.deactivate();
  98. OpenLayers.Control.prototype.destroy.apply(this, arguments);
  99. },
  100. /**
  101. * APIMethod: activate
  102. */
  103. activate: function() {
  104. if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
  105. this.map.events.register('mousemove', this, this.redraw);
  106. this.map.events.register('mouseout', this, this.reset);
  107. this.redraw();
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. },
  113. /**
  114. * APIMethod: deactivate
  115. */
  116. deactivate: function() {
  117. if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) {
  118. this.map.events.unregister('mousemove', this, this.redraw);
  119. this.map.events.unregister('mouseout', this, this.reset);
  120. this.element.innerHTML = "";
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. },
  126. /**
  127. * Method: draw
  128. * {DOMElement}
  129. */
  130. draw: function() {
  131. OpenLayers.Control.prototype.draw.apply(this, arguments);
  132. if (!this.element) {
  133. this.div.left = "";
  134. this.div.top = "";
  135. this.element = this.div;
  136. }
  137. return this.div;
  138. },
  139. /**
  140. * Method: redraw
  141. */
  142. redraw: function(evt) {
  143. var lonLat;
  144. if (evt == null) {
  145. this.reset();
  146. return;
  147. } else {
  148. if (this.lastXy == null ||
  149. Math.abs(evt.xy.x - this.lastXy.x) > this.granularity ||
  150. Math.abs(evt.xy.y - this.lastXy.y) > this.granularity)
  151. {
  152. this.lastXy = evt.xy;
  153. return;
  154. }
  155. lonLat = this.map.getLonLatFromPixel(evt.xy);
  156. if (!lonLat) {
  157. // map has not yet been properly initialized
  158. return;
  159. }
  160. if (this.displayProjection) {
  161. lonLat.transform(this.map.getProjectionObject(),
  162. this.displayProjection );
  163. }
  164. this.lastXy = evt.xy;
  165. }
  166. var newHtml = this.formatOutput(lonLat);
  167. if (newHtml != this.element.innerHTML) {
  168. this.element.innerHTML = newHtml;
  169. }
  170. },
  171. /**
  172. * Method: reset
  173. */
  174. reset: function(evt) {
  175. if (this.emptyString != null) {
  176. this.element.innerHTML = this.emptyString;
  177. }
  178. },
  179. /**
  180. * Method: formatOutput
  181. * Override to provide custom display output
  182. *
  183. * Parameters:
  184. * lonLat - {<OpenLayers.LonLat>} Location to display
  185. */
  186. formatOutput: function(lonLat) {
  187. var digits = parseInt(this.numDigits);
  188. var newHtml =
  189. this.prefix +
  190. lonLat.lon.toFixed(digits) +
  191. this.separator +
  192. lonLat.lat.toFixed(digits) +
  193. this.suffix;
  194. return newHtml;
  195. },
  196. CLASS_NAME: "OpenLayers.Control.MousePosition"
  197. });