Create a gist now

Instantly share code, notes, and snippets.

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

Awesome, thanks a million!

Line 16 in service missing semi-colon ;)

@ximon
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