Source: services/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) service for navigator.geolocation methods
  6. */
  7. /* global google */
  8. (function() {
  9. 'use strict';
  10. var NavigatorGeolocation = function($q) {
  11. return {
  12. /**
  13. * @memberof NavigatorGeolocation
  14. * @param {function} success success callback function
  15. * @param {function} failure failure callback function
  16. * @example
  17. * ```
  18. * NavigatorGeolocation.getCurrentPosition()
  19. * .then(function(position) {
  20. * var lat = position.coords.latitude, lng = position.coords.longitude;
  21. * .. do something lat and lng
  22. * });
  23. * ```
  24. * @returns {HttpPromise} Future object
  25. */
  26. getCurrentPosition: function() {
  27. var deferred = $q.defer();
  28. if (navigator.geolocation) {
  29. navigator.geolocation.getCurrentPosition(
  30. function(position) {
  31. deferred.resolve(position);
  32. }, function(evt) {
  33. console.error(evt);
  34. deferred.reject(evt);
  35. }
  36. );
  37. } else {
  38. deferred.reject("Browser Geolocation service failed.");
  39. }
  40. return deferred.promise;
  41. },
  42. watchPosition: function() {
  43. return "TODO";
  44. },
  45. clearWatch: function() {
  46. return "TODO";
  47. }
  48. };
  49. };
  50. angular.module('ngMap').service('NavigatorGeolocation', ['$q', NavigatorGeolocation]);
  51. })();