Advertisement
Guest User

Untitled

a guest
Sep 4th, 2013
1,657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .factory('geolocation', function ($q, $rootScope, cordovaReady) {
  2.     return {
  3.         getCurrentPosition: cordovaReady(function (onSuccess, onError, options) {
  4.  
  5.             navigator.geolocation.getCurrentPosition(function () {
  6.                 var that = this,
  7.                     args = arguments;
  8.  
  9.                 if (onSuccess) {
  10.                     $rootScope.$apply(function () {
  11.                         onSuccess.apply(that, args);
  12.                     });
  13.                 }
  14.             }, function () {
  15.                 var that = this,
  16.                     args = arguments;
  17.                 if (onError) {
  18.                     $rootScope.$apply(function () {
  19.                         onError.apply(that, args);
  20.                     });
  21.                 }
  22.             }, {
  23.                 enableHighAccuracy: true,
  24.                 timeout: 20000,
  25.                 maximumAge: 18000000
  26.             });
  27.         }),
  28.         getCurrentCity: function () {              
  29.             var deferred = $q.defer();
  30.            
  31.             this.getCurrentPosition(function (position) {
  32.  
  33.                 var geocoder = new google.maps.Geocoder();
  34.                 geocoder.geocode({
  35.                     'latLng': new google.maps.LatLng(parseFloat(position.coords.latitude), parseFloat(position.coords.longitude))
  36.                 },
  37.                 function (results, status) {
  38.                     if (status === google.maps.GeocoderStatus.OK) {
  39.                         if (results[0]) {
  40.                             angular.forEach(results[0].address_components, function (address_component, i) {
  41.                                 if (address_component.types[0] === 'locality') {
  42.                                     $rootScope.$apply(function(){
  43.                                         deferred.resolve(address_component.long_name);
  44.                                    });
  45.                                 }
  46.                            });
  47.                         }
  48.                     }
  49.                 });
  50.             }, function(error){
  51.                 deferred.resolve("fail");
  52.             });
  53.            
  54.             return deferred.promise;
  55.         }
  56.     };
  57. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement