From 9dc5df82fb1c011dbd74806ae1525b055a796126 Mon Sep 17 00:00:00 2001 From: Dmitry Belyaev Date: Thu, 10 Jul 2014 22:15:47 +1000 Subject: [PATCH 1/3] Separate not supported case No use to check for geolocation support on every call. This also improves performace a bit. --- ngGeolocation.js | 107 +++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/ngGeolocation.js b/ngGeolocation.js index 078e9cb..341c951 100644 --- a/ngGeolocation.js +++ b/ngGeolocation.js @@ -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; - }]); \ No newline at end of file + }]); From bd745ee12e186f1bebf13ab5a670ace81b968030 Mon Sep 17 00:00:00 2001 From: Dmitry Belyaev Date: Thu, 10 Jul 2014 23:37:10 +1000 Subject: [PATCH 2/3] Make grunt uglify work --- Gruntfile.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 96f4bed..8c58b70 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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'] } } }, @@ -78,4 +78,4 @@ module.exports = function(grunt) { grunt.registerTask('build', ['concat', 'uglify:src']); grunt.registerTask('default', ['watch:test']); -}; \ No newline at end of file +}; From b1304ff8793f6375559a4f507f42f9159c16e716 Mon Sep 17 00:00:00 2001 From: Dmitry Belyaev Date: Thu, 10 Jul 2014 23:37:35 +1000 Subject: [PATCH 3/3] Update minified version --- ngGeolocation.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ngGeolocation.min.js b/ngGeolocation.min.js index 870abe4..c0719f6 100644 --- a/ngGeolocation.min.js +++ b/ngGeolocation.min.js @@ -1 +1 @@ -"use strict";angular.module("ngGeolocation",[]).factory("$geolocation",["$rootScope","$window","$q",function(a,b,c){function d(){return"geolocation"in b.navigator}var e={getCurrentPosition:function(e){var f=c.defer();return d()?b.navigator.geolocation.getCurrentPosition(function(b){a.$apply(function(){f.resolve(b)})},function(b){a.$apply(function(){f.reject({error:b})})},e):f.reject({error:{code:2,message:"This web browser does not support HTML5 Geolocation"}}),f.promise},watchPosition:function(c){d()?this.watchId||(this.watchId=b.navigator.geolocation.watchPosition(function(b){a.$apply(function(){e.position.coords=b.coords,e.position.timestamp=b.timestamp,delete e.position.error,a.$broadcast("$geolocation.position.changed",b)})},function(b){a.$apply(function(){e.position.error=b,delete e.position.coords,delete e.position.timestamp,a.$broadcast("$geolocation.position.error",b)})},c)):e.position={error:{code:2,message:"This web browser does not support HTML5 Geolocation"}}},clearWatch:function(){this.watchId&&(b.navigator.geolocation.clearWatch(this.watchId),delete this.watchId)},position:{}};return e}]); \ No newline at end of file +"use strict";angular.module("ngGeolocation",[]).factory("$geolocation",["$rootScope","$window","$q",function(a,b,c){if(!b.navigator||!b.navigator.geolocation)return{supported:!1,getCurrentPosition:function(){var a=c.defer();return a.reject(this.position),a.promise},watchPosition:angular.noop,clearWatch:angular.noop,position:{error:{code:2,message:"This web browser does not support HTML5 Geolocation"}}};var d=null,e={supported:!0,getCurrentPosition:function(a){var d=c.defer();return b.navigator.geolocation.getCurrentPosition(function(a){angular.copy(a,e.position),d.resolve(a)},function(a){d.reject({error:a})},a),d.promise},watchPosition:function(c){return d?!1:(d=b.navigator.geolocation.watchPosition(function(b){a.$apply(function(){angular.copy(b,e.position),a.$broadcast("$geolocation.position.changed",b)})},function(b){a.$apply(function(){angular.copy({error:b},e.position),a.$broadcast("$geolocation.position.error",b)})},c),!0)},clearWatch:function(){d&&(b.navigator.geolocation.clearWatch(d),d=null)},position:{}};return e}]); \ No newline at end of file