geolocation.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. var argscheck = require('cordova/argscheck');
  22. var utils = require('cordova/utils');
  23. var exec = require('cordova/exec');
  24. var PositionError = require('./PositionError');
  25. var Position = require('./Position');
  26. var timers = {}; // list of timers in use
  27. // Returns default params, overrides if provided with values
  28. function parseParameters (options) {
  29. var opt = {
  30. maximumAge: 0,
  31. enableHighAccuracy: false,
  32. timeout: Infinity
  33. };
  34. if (options) {
  35. if (options.maximumAge !== undefined && !isNaN(options.maximumAge) && options.maximumAge > 0) {
  36. opt.maximumAge = options.maximumAge;
  37. }
  38. if (options.enableHighAccuracy !== undefined) {
  39. opt.enableHighAccuracy = options.enableHighAccuracy;
  40. }
  41. if (options.timeout !== undefined && !isNaN(options.timeout)) {
  42. if (options.timeout < 0) {
  43. opt.timeout = 0;
  44. } else {
  45. opt.timeout = options.timeout;
  46. }
  47. }
  48. }
  49. return opt;
  50. }
  51. // Returns a timeout failure, closed over a specified timeout value and error callback.
  52. function createTimeout (errorCallback, timeout) {
  53. var t = setTimeout(function () {
  54. clearTimeout(t);
  55. t = null;
  56. errorCallback({
  57. code: PositionError.TIMEOUT,
  58. message: 'Position retrieval timed out.'
  59. });
  60. }, timeout);
  61. return t;
  62. }
  63. var geolocation = {
  64. lastPosition: null, // reference to last known (cached) position returned
  65. /**
  66. * Asynchronously acquires the current position.
  67. *
  68. * @param {Function} successCallback The function to call when the position data is available
  69. * @param {Function} errorCallback The function to call when there is an error getting the heading position. (OPTIONAL)
  70. * @param {PositionOptions} options The options for getting the position data. (OPTIONAL)
  71. */
  72. getCurrentPosition: function (successCallback, errorCallback, options) {
  73. argscheck.checkArgs('fFO', 'geolocation.getCurrentPosition', arguments);
  74. options = parseParameters(options);
  75. // Timer var that will fire an error callback if no position is retrieved from native
  76. // before the "timeout" param provided expires
  77. var timeoutTimer = {timer: null};
  78. var win = function (p) {
  79. clearTimeout(timeoutTimer.timer);
  80. if (!(timeoutTimer.timer)) {
  81. // Timeout already happened, or native fired error callback for
  82. // this geo request.
  83. // Don't continue with success callback.
  84. return;
  85. }
  86. var pos = new Position(
  87. {
  88. latitude: p.latitude,
  89. longitude: p.longitude,
  90. altitude: p.altitude,
  91. accuracy: p.accuracy,
  92. heading: p.heading,
  93. velocity: p.velocity,
  94. altitudeAccuracy: p.altitudeAccuracy
  95. },
  96. p.timestamp
  97. );
  98. geolocation.lastPosition = pos;
  99. successCallback(pos);
  100. };
  101. var fail = function (e) {
  102. clearTimeout(timeoutTimer.timer);
  103. timeoutTimer.timer = null;
  104. var err = new PositionError(e.code, e.message);
  105. if (errorCallback) {
  106. errorCallback(err);
  107. }
  108. };
  109. // Check our cached position, if its timestamp difference with current time is less than the maximumAge, then just
  110. // fire the success callback with the cached position.
  111. if (geolocation.lastPosition && options.maximumAge && (((new Date()).getTime() - geolocation.lastPosition.timestamp) <= options.maximumAge)) {
  112. successCallback(geolocation.lastPosition);
  113. // If the cached position check failed and the timeout was set to 0, error out with a TIMEOUT error object.
  114. } else if (options.timeout === 0) {
  115. fail({
  116. code: PositionError.TIMEOUT,
  117. message: "timeout value in PositionOptions set to 0 and no cached Position object available, or cached Position object's age exceeds provided PositionOptions' maximumAge parameter."
  118. });
  119. // Otherwise we have to call into native to retrieve a position.
  120. } else {
  121. if (options.timeout !== Infinity) {
  122. // If the timeout value was not set to Infinity (default), then
  123. // set up a timeout function that will fire the error callback
  124. // if no successful position was retrieved before timeout expired.
  125. timeoutTimer.timer = createTimeout(fail, options.timeout);
  126. } else {
  127. // This is here so the check in the win function doesn't mess stuff up
  128. // may seem weird but this guarantees timeoutTimer is
  129. // always truthy before we call into native
  130. timeoutTimer.timer = true;
  131. }
  132. exec(win, fail, 'Geolocation', 'getLocation', [options.enableHighAccuracy, options.maximumAge]);
  133. }
  134. return timeoutTimer;
  135. },
  136. /**
  137. * Asynchronously watches the geolocation for changes to geolocation. When a change occurs,
  138. * the successCallback is called with the new location.
  139. *
  140. * @param {Function} successCallback The function to call each time the location data is available
  141. * @param {Function} errorCallback The function to call when there is an error getting the location data. (OPTIONAL)
  142. * @param {PositionOptions} options The options for getting the location data such as frequency. (OPTIONAL)
  143. * @return String The watch id that must be passed to #clearWatch to stop watching.
  144. */
  145. watchPosition: function (successCallback, errorCallback, options) {
  146. argscheck.checkArgs('fFO', 'geolocation.getCurrentPosition', arguments);
  147. options = parseParameters(options);
  148. var id = utils.createUUID();
  149. // Tell device to get a position ASAP, and also retrieve a reference to the timeout timer generated in getCurrentPosition
  150. timers[id] = geolocation.getCurrentPosition(successCallback, errorCallback, options);
  151. var fail = function (e) {
  152. clearTimeout(timers[id].timer);
  153. var err = new PositionError(e.code, e.message);
  154. if (errorCallback) {
  155. errorCallback(err);
  156. }
  157. };
  158. var win = function (p) {
  159. clearTimeout(timers[id].timer);
  160. if (options.timeout !== Infinity) {
  161. timers[id].timer = createTimeout(fail, options.timeout);
  162. }
  163. var pos = new Position(
  164. {
  165. latitude: p.latitude,
  166. longitude: p.longitude,
  167. altitude: p.altitude,
  168. accuracy: p.accuracy,
  169. heading: p.heading,
  170. velocity: p.velocity,
  171. altitudeAccuracy: p.altitudeAccuracy
  172. },
  173. p.timestamp
  174. );
  175. geolocation.lastPosition = pos;
  176. successCallback(pos);
  177. };
  178. exec(win, fail, 'Geolocation', 'addWatch', [id, options.enableHighAccuracy]);
  179. return id;
  180. },
  181. /**
  182. * Clears the specified heading watch.
  183. *
  184. * @param {String} id The ID of the watch returned from #watchPosition
  185. */
  186. clearWatch: function (id) {
  187. if (id && timers[id] !== undefined) {
  188. clearTimeout(timers[id].timer);
  189. timers[id].timer = false;
  190. exec(null, null, 'Geolocation', 'clearWatch', [id]);
  191. }
  192. }
  193. };
  194. module.exports = geolocation;