Geolocate.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * @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. * Constant: EVENT_TYPES
  24. * Supported event types:
  25. * - *locationupdated* Triggered when browser return a new position
  26. * - *locationfailed* Triggered when geolocation has failed
  27. * - *locationuncapable* Triggered when control is activated on a browser
  28. * which doesn't support geolocation
  29. */
  30. EVENT_TYPES: ["locationupdated", "locationfailed", "locationuncapable"],
  31. /**
  32. * Property: geolocation
  33. * {Object} The geolocation engine, as a property to be possibly mocked.
  34. */
  35. geolocation: navigator.geolocation,
  36. /**
  37. * APIProperty: bind
  38. * {Boolean} If true, map center will be set on location update.
  39. */
  40. bind: true,
  41. /**
  42. * APIProperty: watch
  43. * {Boolean} If true, position will be update regularly.
  44. */
  45. watch: false,
  46. /**
  47. * APIProperty: geolocationOptions
  48. * {Object} Options to pass to the navigator's geolocation API. See
  49. * <http://dev.w3.org/geo/api/spec-source.html>. No specific
  50. * option is passed to the geolocation API by default.
  51. */
  52. geolocationOptions: null,
  53. /**
  54. * Constructor: OpenLayers.Control.Geolocate
  55. * Create a new control to deal with browser geolocation API
  56. *
  57. */
  58. initialize: function(options) {
  59. // concatenate events specific to this control with those from the base
  60. this.EVENT_TYPES =
  61. OpenLayers.Control.Geolocate.prototype.EVENT_TYPES.concat(
  62. OpenLayers.Control.prototype.EVENT_TYPES
  63. );
  64. this.geolocationOptions = {};
  65. OpenLayers.Control.prototype.initialize.apply(this, [options]);
  66. },
  67. /**
  68. * Method: destroy
  69. */
  70. destroy: function() {
  71. this.deactivate();
  72. OpenLayers.Control.prototype.destroy.apply(this, arguments);
  73. },
  74. /**
  75. * Method: activate
  76. * Activates the control.
  77. *
  78. * Returns:
  79. * {Boolean} The control was effectively activated.
  80. */
  81. activate: function () {
  82. if (!this.geolocation) {
  83. this.events.triggerEvent("locationuncapable");
  84. return false;
  85. }
  86. if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
  87. if (this.watch) {
  88. this.watchId = this.geolocation.watchPosition(
  89. OpenLayers.Function.bind(this.geolocate, this),
  90. OpenLayers.Function.bind(this.failure, this),
  91. this.geolocationOptions
  92. );
  93. } else {
  94. this.getCurrentLocation();
  95. }
  96. return true;
  97. }
  98. return false;
  99. },
  100. /**
  101. * Method: deactivate
  102. * Deactivates the control.
  103. *
  104. * Returns:
  105. * {Boolean} The control was effectively deactivated.
  106. */
  107. deactivate: function () {
  108. if (this.active && this.watchId !== null) {
  109. this.geolocation.clearWatch(this.watchId);
  110. }
  111. return OpenLayers.Control.prototype.deactivate.apply(
  112. this, arguments
  113. );
  114. },
  115. /**
  116. * Method: geolocate
  117. * Activates the control.
  118. *
  119. */
  120. geolocate: function (position) {
  121. var center = new OpenLayers.LonLat(
  122. position.coords.longitude,
  123. position.coords.latitude
  124. ).transform(
  125. new OpenLayers.Projection("EPSG:4326"),
  126. this.map.getProjectionObject()
  127. );
  128. if (this.bind) {
  129. this.map.setCenter(center);
  130. }
  131. this.events.triggerEvent("locationupdated", {
  132. position: position,
  133. point: new OpenLayers.Geometry.Point(
  134. center.lon, center.lat
  135. )
  136. });
  137. },
  138. /**
  139. * APIMethod: getCurrentLocation
  140. *
  141. * Returns:
  142. * {Boolean} Returns true if a event will be fired (successfull
  143. * registration)
  144. */
  145. getCurrentLocation: function() {
  146. if (!this.active || this.watch) {
  147. return false;
  148. }
  149. this.geolocation.getCurrentPosition(
  150. OpenLayers.Function.bind(this.geolocate, this),
  151. OpenLayers.Function.bind(this.failure, this),
  152. this.geolocationOptions
  153. );
  154. return true;
  155. },
  156. /**
  157. * Method: failure
  158. * method called on browser's geolocation failure
  159. *
  160. */
  161. failure: function (error) {
  162. this.events.triggerEvent("locationfailed", {error: error});
  163. },
  164. CLASS_NAME: "OpenLayers.Control.Geolocate"
  165. });