Geolocate.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. * @requires OpenLayers/Geometry/Point.js
  8. * @requires OpenLayers/Projection.js
  9. */
  10. /**
  11. * Class: OpenLayers.Control.Geolocate
  12. * The Geolocate control wraps w3c geolocation API into control that can be
  13. * bound to a map, and generate events on location update
  14. *
  15. * To use this control requires to load the proj4js library if the projection
  16. * of the map is not EPSG:4326 or EPSG:900913.
  17. *
  18. * Inherits from:
  19. * - <OpenLayers.Control>
  20. */
  21. OpenLayers.Control.Geolocate = OpenLayers.Class(OpenLayers.Control, {
  22. /**
  23. * APIProperty: events
  24. * {<OpenLayers.Events>} Events instance for listeners and triggering
  25. * control specific events.
  26. *
  27. * Register a listener for a particular event with the following syntax:
  28. * (code)
  29. * control.events.register(type, obj, listener);
  30. * (end)
  31. *
  32. * Supported event types (in addition to those from <OpenLayers.Control.events>):
  33. * locationupdated - Triggered when browser return a new position. Listeners will
  34. * receive an object with a 'position' property which is the browser.geolocation.position
  35. * native object, as well as a 'point' property which is the location transformed in the
  36. * current map projection.
  37. * locationfailed - Triggered when geolocation has failed
  38. * locationuncapable - Triggered when control is activated on a browser
  39. * which doesn't support geolocation
  40. */
  41. /**
  42. * Property: geolocation
  43. * {Object} The geolocation engine, as a property to be possibly mocked.
  44. * This is set lazily to avoid a memory leak in IE9.
  45. */
  46. geolocation: null,
  47. /**
  48. * Property: available
  49. * {Boolean} The navigator.geolocation object is available.
  50. */
  51. available: ('geolocation' in navigator),
  52. /**
  53. * APIProperty: bind
  54. * {Boolean} If true, map center will be set on location update.
  55. */
  56. bind: true,
  57. /**
  58. * APIProperty: watch
  59. * {Boolean} If true, position will be update regularly.
  60. */
  61. watch: false,
  62. /**
  63. * APIProperty: geolocationOptions
  64. * {Object} Options to pass to the navigator's geolocation API. See
  65. * <http://dev.w3.org/geo/api/spec-source.html>. No specific
  66. * option is passed to the geolocation API by default.
  67. */
  68. geolocationOptions: null,
  69. /**
  70. * Constructor: OpenLayers.Control.Geolocate
  71. * Create a new control to deal with browser geolocation API
  72. *
  73. */
  74. /**
  75. * Method: destroy
  76. */
  77. destroy: function() {
  78. this.deactivate();
  79. OpenLayers.Control.prototype.destroy.apply(this, arguments);
  80. },
  81. /**
  82. * Method: activate
  83. * Activates the control.
  84. *
  85. * Returns:
  86. * {Boolean} The control was effectively activated.
  87. */
  88. activate: function () {
  89. if (this.available && !this.geolocation) {
  90. // set lazily to avoid IE9 memory leak
  91. this.geolocation = navigator.geolocation;
  92. }
  93. if (!this.geolocation) {
  94. this.events.triggerEvent("locationuncapable");
  95. return false;
  96. }
  97. if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
  98. if (this.watch) {
  99. this.watchId = this.geolocation.watchPosition(
  100. OpenLayers.Function.bind(this.geolocate, this),
  101. OpenLayers.Function.bind(this.failure, this),
  102. this.geolocationOptions
  103. );
  104. } else {
  105. this.getCurrentLocation();
  106. }
  107. return true;
  108. }
  109. return false;
  110. },
  111. /**
  112. * Method: deactivate
  113. * Deactivates the control.
  114. *
  115. * Returns:
  116. * {Boolean} The control was effectively deactivated.
  117. */
  118. deactivate: function () {
  119. if (this.active && this.watchId !== null) {
  120. this.geolocation.clearWatch(this.watchId);
  121. }
  122. return OpenLayers.Control.prototype.deactivate.apply(
  123. this, arguments
  124. );
  125. },
  126. /**
  127. * Method: geolocate
  128. * Activates the control.
  129. *
  130. */
  131. geolocate: function (position) {
  132. var center = new OpenLayers.LonLat(
  133. position.coords.longitude,
  134. position.coords.latitude
  135. ).transform(
  136. new OpenLayers.Projection("EPSG:4326"),
  137. this.map.getProjectionObject()
  138. );
  139. if (this.bind) {
  140. this.map.setCenter(center);
  141. }
  142. this.events.triggerEvent("locationupdated", {
  143. position: position,
  144. point: new OpenLayers.Geometry.Point(
  145. center.lon, center.lat
  146. )
  147. });
  148. },
  149. /**
  150. * APIMethod: getCurrentLocation
  151. *
  152. * Returns:
  153. * {Boolean} Returns true if a event will be fired (successfull
  154. * registration)
  155. */
  156. getCurrentLocation: function() {
  157. if (!this.active || this.watch) {
  158. return false;
  159. }
  160. this.geolocation.getCurrentPosition(
  161. OpenLayers.Function.bind(this.geolocate, this),
  162. OpenLayers.Function.bind(this.failure, this),
  163. this.geolocationOptions
  164. );
  165. return true;
  166. },
  167. /**
  168. * Method: failure
  169. * method called on browser's geolocation failure
  170. *
  171. */
  172. failure: function (error) {
  173. this.events.triggerEvent("locationfailed", {error: error});
  174. },
  175. CLASS_NAME: "OpenLayers.Control.Geolocate"
  176. });