source : navigator-geolocation.js

  1. /**
  2. * @ngdoc service
  3. * @name NavigatorGeolocation
  4. * @description
  5. * Provides [defered/promise API](https://docs.angularjs.org/api/ng/service/$q)
  6. * service for navigator.geolocation methods
  7. */
  8. /* global google */
  9. (function() {
  10. 'use strict';
  11. var $q;
  12. /**
  13. * @memberof NavigatorGeolocation
  14. * @param {Object} geoLocationOptions the navigator geolocations options.
  15. * i.e. { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true }.
  16. * If none specified, { timeout: 5000 }.
  17. * If timeout not specified, timeout: 5000 added
  18. * @param {function} success success callback function
  19. * @param {function} failure failure callback function
  20. * @example
  21. * ```
  22. * NavigatorGeolocation.getCurrentPosition()
  23. * .then(function(position) {
  24. * var lat = position.coords.latitude, lng = position.coords.longitude;
  25. * .. do something lat and lng
  26. * });
  27. * ```
  28. * @returns {HttpPromise} Future object
  29. */
  30. var getCurrentPosition = function(geoLocationOptions) {
  31. var deferred = $q.defer();
  32. if (navigator.geolocation) {
  33. if (geoLocationOptions === undefined) {
  34. geoLocationOptions = { timeout: 5000 };
  35. }
  36. else if (geoLocationOptions.timeout === undefined) {
  37. geoLocationOptions.timeout = 5000;
  38. }
  39. navigator.geolocation.getCurrentPosition(
  40. function(position) {
  41. deferred.resolve(position);
  42. }, function(evt) {
  43. console.error(evt);
  44. deferred.reject(evt);
  45. },
  46. geoLocationOptions
  47. );
  48. } else {
  49. deferred.reject("Browser Geolocation service failed.");
  50. }
  51. return deferred.promise;
  52. };
  53. var NavigatorGeolocation = function(_$q_) {
  54. $q = _$q_;
  55. return {
  56. getCurrentPosition: getCurrentPosition
  57. };
  58. };
  59. NavigatorGeolocation.$inject = ['$q'];
  60. angular.module('ngMap').
  61. service('NavigatorGeolocation', NavigatorGeolocation);
  62. })();