navigator_geolocation.js 1.3 KB

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