Skip to content

Instantly share code, notes, and snippets.

@robhurring
Last active March 3, 2016 11:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robhurring/6074128 to your computer and use it in GitHub Desktop.
Save robhurring/6074128 to your computer and use it in GitHub Desktop.
Angular geolocation wrapper service
app.factory("GeolocationService", ['$q', '$window', '$rootScope', function($q, $window, $rootScope){
return function(){
var deferred = $q.defer();
if(!$window.navigator) {
deferred.reject("Geolocation is not supported");
} else {
$window.navigator.geolocation.getCurrentPosition(function(position) {
$rootScope.$apply(function(){
deferred.resolve(position);
});
});
}
return deferred.promise;
}
}]);
app.controller('MainCtrl', ['$scope','GeolocationService', function($scope, geolocation){
$scope.position = null;
geolocation().then(function(position) {
$scope.position = position;
});
}]);
@vredchenko
Copy link

Awesome, thanks a million!

Line 16 in service missing semi-colon ;)

@ximon
Copy link

ximon commented Mar 3, 2016

Should if (!$window.navigator) be if (!$window.navigator.geolocation) ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment