Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = function(grunt) {
uglify: {
src: {
files: {
'<%= pkg.name %>.min.js': ['<%= pkg.name %>.min.js']
'<%= pkg.name %>.min.js': ['<%= pkg.name %>.js']
}
}
},
Expand Down Expand Up @@ -78,4 +78,4 @@ module.exports = function(grunt) {
grunt.registerTask('build', ['concat', 'uglify:src']);

grunt.registerTask('default', ['watch:test']);
};
};
107 changes: 53 additions & 54 deletions ngGeolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,75 @@

angular
.module('ngGeolocation', [])
.factory('$geolocation', ['$rootScope', '$window', '$q', function($rootScope, $window, $q) {

function supported() {
return 'geolocation' in $window.navigator;
.factory('$geolocation', ['$rootScope','$window','$q',
function($rootScope, $window, $q)
{
if (!$window.navigator || !$window.navigator.geolocation) {
return {
supported: false,
getCurrentPosition: function() {
var deferred = $q.defer();
deferred.reject(this.position);
return deferred.promise;
},
watchPosition: angular.noop,
clearWatch: angular.noop,
position: {
error: {
code: 2,
message: 'This web browser does not support HTML5 Geolocation'
}
}
};
}

var watchId = null;
var retVal = {
supported: true,

getCurrentPosition: function(options) {
var deferred = $q.defer();
if(supported()) {
$window.navigator.geolocation.getCurrentPosition(
function(position) {
$rootScope.$apply(function() {
deferred.resolve(position);
});
},
function(error) {
$rootScope.$apply(function() {
deferred.reject({error: error});
});
}, options);
} else {
deferred.reject({error: {
code: 2,
message: 'This web browser does not support HTML5 Geolocation'
}});
}
$window.navigator.geolocation.getCurrentPosition(
function(position) {
angular.copy(position, retVal.position);
deferred.resolve(position);
},
function(error) {
deferred.reject({error: error});
}, options);
return deferred.promise;
},

watchPosition: function(options) {
if(supported()) {
if(!this.watchId) {
this.watchId = $window.navigator.geolocation.watchPosition(
function(position) {
$rootScope.$apply(function() {
retVal.position.coords = position.coords;
retVal.position.timestamp = position.timestamp;
delete retVal.position.error;
$rootScope.$broadcast('$geolocation.position.changed', position);
});
},
function(error) {
$rootScope.$apply(function() {
retVal.position.error = error;
delete retVal.position.coords;
delete retVal.position.timestamp;
$rootScope.$broadcast('$geolocation.position.error', error);
});
}, options);
}
} else {
retVal.position = {
error: {
code: 2,
message: 'This web browser does not support HTML5 Geolocation'
}
};
if (watchId) {
return false;
}

watchId = $window.navigator.geolocation.watchPosition(
function(position) {
$rootScope.$apply(function() {
angular.copy(position, retVal.position);
$rootScope.$broadcast('$geolocation.position.changed', position);
});
},
function(error) {
$rootScope.$apply(function() {
angular.copy({error: error}, retVal.position);
$rootScope.$broadcast('$geolocation.position.error', error);
});
}, options);

return true;
},

clearWatch: function() {
if(this.watchId) {
$window.navigator.geolocation.clearWatch(this.watchId);
delete this.watchId;
if (watchId) {
$window.navigator.geolocation.clearWatch(watchId);
watchId = null;
}
},

position: {}
};

return retVal;
}]);
}]);
2 changes: 1 addition & 1 deletion ngGeolocation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.