-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to support async loading of geo libs
- Loading branch information
Showing
2 changed files
with
188 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,141 @@ | ||
/** | ||
* Created by Mike on 1/31/14. | ||
*/ | ||
|
||
(function() { | ||
"use strict"; | ||
|
||
var angularGeo = angular.module('angular-geo', []); | ||
angularGeo.constant('angularGeo_msgs', { | ||
'errors.unsupportedBrowser':'Browser geolocation not supported', | ||
'errors.noServices':'angularGeo could not locate any of the geo providers specified, verify that you have them properly linked', | ||
'errors.noProviders':'angularGeo requires at least one geo provider' | ||
}); | ||
angularGeo.provider('angularGeo', function () { | ||
var $$geoConfig = { | ||
providerSvcNames: [], | ||
providers: [] | ||
}; | ||
var $$supportsGeolocation = false; | ||
var $$watchId; | ||
var $$currentProvider = 0; | ||
|
||
return { | ||
addProvider: function (providerSvcName) { | ||
$$geoConfig.providerSvcNames.push(providerSvcName); | ||
return this; | ||
}, | ||
$get: function angularGeo($log, $q, $timeout, $rootScope, $injector, $window, angularGeo_msgs) { | ||
|
||
$$supportsGeolocation = 'geolocation' in $window.navigator; | ||
|
||
if($$geoConfig.providerSvcNames.length === 0) { | ||
throw new Error(angularGeo_msgs.errors.noProviders); | ||
} | ||
// Instantiate the geo providers and store them within the $$geoConfig object | ||
for(var i = 0; i < $$geoConfig.providerSvcNames.length; i++) { | ||
if(!$injector.has($$geoConfig.providerSvcNames[i])) { | ||
$log.error('angularGeo could not locate service ' + $$geoConfig.providerSvcNames[i]); | ||
} else { | ||
$$geoConfig.providers.push($injector.get($$geoConfig.providerSvcNames[i])); | ||
} | ||
} | ||
var $$providers = $$geoConfig.providers; | ||
if($$providers.length === 0) { | ||
throw new Error(angularGeo_msgs.errors.noServices); | ||
} | ||
|
||
var $$geocode = function(address, bounds, region, restrictions, filters, promise) { | ||
var deferred = promise || $q.defer(); | ||
var p = $$providers[$$currentProvider].geocode(address, bounds, region, restrictions, filters); | ||
p.then(function(results) { | ||
deferred.resolve(results); | ||
}, function(err) { | ||
if($$providers.length === 1) { | ||
deferred.reject(err); | ||
return; | ||
} | ||
if($$currentProvider < $$providers.length - 1) { | ||
$$currentProvider++; | ||
} else { | ||
$$currentProvider = 0; | ||
} | ||
return $$geocode(address, bounds, region, restrictions, filters, deferred); | ||
}); | ||
return deferred.promise; | ||
}; | ||
|
||
var $$reverseGeo = function(latLng, bounds, region, restrictions, filters, promise) { | ||
var deferred = promise || $q.defer(); | ||
var p = $$providers[$$currentProvider].reverseGeocode(latLng, bounds, region, restrictions, filters); | ||
p.then(function(results) { | ||
deferred.resolve(results); | ||
}, function(err) { | ||
if($$providers.length === 1) { | ||
deferred.reject(err); | ||
return; | ||
} | ||
if($$currentProvider < $$providers.length - 1) { | ||
$$currentProvider++; | ||
} else { | ||
$$currentProvider = 0; | ||
} | ||
return $$reverseGeo(latLng, bounds, region, restrictions, filters, deferred); | ||
}); | ||
return deferred.promise; | ||
}; | ||
|
||
return { | ||
geocode: function(address, bounds, region, restrictions, filters) { | ||
return $$geocode(address, bounds, region, restrictions, filters, null); | ||
}, | ||
reverseGeocode: function(latLng, bounds, region, restrictions, filters) { | ||
return $$reverseGeo(latLng, bounds, region, restrictions, filters); | ||
}, | ||
getCurrentPosition: function(options, autoReverseGeocode) { | ||
var self = this; | ||
if(!$$supportsGeolocation) { | ||
throw new Error(angularGeo_msgs.errors.unsupportedBrowser); | ||
} | ||
var deferred = $q.defer(); | ||
$window.navigator.geolocation.getCurrentPosition(function(pos) { | ||
if(autoReverseGeocode) { | ||
var p = $$reverseGeo({latitude: pos.coords.latitude, longitude: pos.coords.longitude}, null, null, null, null); | ||
p.then(function(results) { | ||
deferred.resolve(results); | ||
}, function(err) { | ||
deferred.reject(err); | ||
}); | ||
} else { | ||
deferred.resolve(pos); | ||
} | ||
}, function(err) { | ||
deferred.reject(err); | ||
}, options); | ||
return deferred.promise; | ||
}, | ||
watchPosition: function(options) { | ||
var self = this; | ||
if(!$$supportsGeolocation) { | ||
throw new Error(angularGeo_msgs.errors.unsupportedBrowser); | ||
} | ||
var deferred = $q.defer(); | ||
$$watchId = $window.navigator.geolocation.watchPosition(function(pos) { | ||
$rootScope.$broadcast("angulargeo:watchposition", pos); | ||
}, function(err) { | ||
deferred.reject(err); | ||
}, options); | ||
return deferred.promise; | ||
|
||
}, | ||
clearWatch: function() { | ||
if(!$$supportsGeolocation) { | ||
throw new Error(angularGeo_msgs.errors.unsupportedBrowser); | ||
} | ||
$window.navigator.geolocation.clearWatch($$watchId); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}).call(this); | ||
/** | ||
* Created by Mike on 1/31/14. | ||
*/ | ||
|
||
(function() { | ||
"use strict"; | ||
|
||
var angularGeo = angular.module('angular-geo', []); | ||
angularGeo.constant('angularGeo_msgs', { | ||
'errors.unsupportedBrowser':'Browser geolocation not supported', | ||
'errors.noServices':'angularGeo could not locate any of the geo providers specified, verify that you have them properly linked', | ||
'errors.noProviders':'angularGeo requires at least one geo provider' | ||
}); | ||
angularGeo.provider('angularGeo', function () { | ||
var $$geoConfig = { | ||
providerSvcNames: [], | ||
providers: [] | ||
}; | ||
var $$supportsGeolocation = false; | ||
var $$watchId; | ||
var $$currentProvider = 0; | ||
|
||
return { | ||
addProvider: function (providerSvcName) { | ||
$$geoConfig.providerSvcNames.push(providerSvcName); | ||
return this; | ||
}, | ||
$get: function angularGeo($log, $q, $timeout, $rootScope, $injector, $window, angularGeo_msgs) { | ||
|
||
$$supportsGeolocation = 'geolocation' in $window.navigator; | ||
|
||
if($$geoConfig.providerSvcNames.length === 0) { | ||
throw new Error(angularGeo_msgs.errors.noProviders); | ||
} | ||
// Instantiate the geo providers and store them within the $$geoConfig object | ||
for(var i = 0; i < $$geoConfig.providerSvcNames.length; i++) { | ||
if(!$injector.has($$geoConfig.providerSvcNames[i])) { | ||
$log.error('angularGeo could not locate service ' + $$geoConfig.providerSvcNames[i]); | ||
} else { | ||
$$geoConfig.providers.push($injector.get($$geoConfig.providerSvcNames[i])); | ||
} | ||
} | ||
var $$providers = $$geoConfig.providers; | ||
if($$providers.length === 0) { | ||
throw new Error(angularGeo_msgs.errors.noServices); | ||
} | ||
|
||
var $$geocode = function(address, bounds, region, restrictions, filters, promise) { | ||
var deferred = promise || $q.defer(); | ||
var p = $$providers[$$currentProvider].geocode(address, bounds, region, restrictions, filters); | ||
p.then(function(results) { | ||
deferred.resolve(results); | ||
}, function(err) { | ||
if($$providers.length === 1) { | ||
deferred.reject(err); | ||
return; | ||
} | ||
if($$currentProvider < $$providers.length - 1) { | ||
$$currentProvider++; | ||
} else { | ||
$$currentProvider = 0; | ||
} | ||
return $$geocode(address, bounds, region, restrictions, filters, deferred); | ||
}); | ||
return deferred.promise; | ||
}; | ||
|
||
var $$reverseGeo = function(latLng, bounds, region, restrictions, filters, promise) { | ||
var deferred = promise || $q.defer(); | ||
var p = $$providers[$$currentProvider].reverseGeocode(latLng, bounds, region, restrictions, filters); | ||
p.then(function(results) { | ||
deferred.resolve(results); | ||
}, function(err) { | ||
if($$providers.length === 1) { | ||
deferred.reject(err); | ||
return; | ||
} | ||
if($$currentProvider < $$providers.length - 1) { | ||
$$currentProvider++; | ||
} else { | ||
$$currentProvider = 0; | ||
} | ||
return $$reverseGeo(latLng, bounds, region, restrictions, filters, deferred); | ||
}); | ||
return deferred.promise; | ||
}; | ||
|
||
return { | ||
geocode: function(address, bounds, region, restrictions, filters) { | ||
return $$geocode(address, bounds, region, restrictions, filters, null); | ||
}, | ||
reverseGeocode: function(latLng, bounds, region, restrictions, filters) { | ||
return $$reverseGeo(latLng, bounds, region, restrictions, filters); | ||
}, | ||
getCurrentPosition: function(options, autoReverseGeocode) { | ||
var self = this; | ||
if(!$$supportsGeolocation) { | ||
throw new Error(angularGeo_msgs.errors.unsupportedBrowser); | ||
} | ||
var deferred = $q.defer(); | ||
$window.navigator.geolocation.getCurrentPosition(function(pos) { | ||
if(autoReverseGeocode) { | ||
var p = $$reverseGeo({latitude: pos.coords.latitude, longitude: pos.coords.longitude}, null, null, null, null); | ||
p.then(function(results) { | ||
deferred.resolve(results); | ||
}, function(err) { | ||
deferred.reject(err); | ||
}); | ||
} else { | ||
deferred.resolve(pos); | ||
} | ||
}, function(err) { | ||
deferred.reject(err); | ||
}, options); | ||
return deferred.promise; | ||
}, | ||
watchPosition: function(options) { | ||
var self = this; | ||
if(!$$supportsGeolocation) { | ||
throw new Error(angularGeo_msgs.errors.unsupportedBrowser); | ||
} | ||
var deferred = $q.defer(); | ||
$$watchId = $window.navigator.geolocation.watchPosition(function(pos) { | ||
$rootScope.$broadcast("angulargeo:watchposition", pos); | ||
}, function(err) { | ||
deferred.reject(err); | ||
}, options); | ||
return deferred.promise; | ||
|
||
}, | ||
clearWatch: function() { | ||
if(!$$supportsGeolocation) { | ||
throw new Error(angularGeo_msgs.errors.unsupportedBrowser); | ||
} | ||
$window.navigator.geolocation.clearWatch($$watchId); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}).call(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
/** | ||
* Created by Mike on 2/02/14. | ||
*/ | ||
(function() { | ||
"use strict"; | ||
angular.module('angular-geo-providers.bing', []) | ||
.provider('angularGeoBing', function() { | ||
var $$q, $$log; | ||
var $$configuration = {}; | ||
var $$http; | ||
|
||
var $$geoEndpoint = "https://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}&jsonp=JSON_CALLBACK"; | ||
var $$rgeoEndpoint = "https://dev.virtualearth.net/REST/v1/Locations/{0}&key={1}&jsonp=JSON_CALLBACK"; | ||
|
||
var svc = { | ||
geocode: function(address, bounds, region, restrictions, filters) { | ||
var deferred = $$q.defer(); | ||
var httpPromise = $$http.jsonp($$geoEndpoint.replace("{0}", address)); | ||
httpPromise.success(function(data, status, headers, config) { | ||
deferred.resolve(data); | ||
}) | ||
.error(function(data, status, headers, config) { | ||
deferred.reject({data: data, status: status}); | ||
}); | ||
|
||
return deferred.promise; | ||
}, | ||
reverseGeocode: function(latLng, bounds, region, restrictions, filters) { | ||
|
||
} | ||
}; | ||
return { | ||
name: 'angularGeoBing', | ||
config: function(config) { | ||
$$configuration.apiKey = config.apiKey; | ||
$$geoEndpoint = $$geoEndpoint.replace("{1}", $$configuration.apiKey); | ||
$$rgeoEndpoint = $$rgeoEndpoint.replace("{1}", $$configuration.apiKey); | ||
}, | ||
$get: function($log, $q, $http) { | ||
$$q = $q; | ||
$$log = $log; | ||
$$http = $http; | ||
return svc; | ||
} | ||
} | ||
}); | ||
}).call(this); | ||
/** | ||
* Created by Mike on 2/02/14. | ||
*/ | ||
(function() { | ||
"use strict"; | ||
angular.module('angular-geo-providers.bing', []) | ||
.provider('angularGeoBing', function() { | ||
var $$q, $$log; | ||
var $$configuration = {}; | ||
var $$http; | ||
|
||
var $$geoEndpoint = "https://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}&jsonp=JSON_CALLBACK"; | ||
var $$rgeoEndpoint = "https://dev.virtualearth.net/REST/v1/Locations/{0}&key={1}&jsonp=JSON_CALLBACK"; | ||
|
||
var svc = { | ||
geocode: function(address, bounds, region, restrictions, filters) { | ||
var deferred = $$q.defer(); | ||
var httpPromise = $$http.jsonp($$geoEndpoint.replace("{0}", address)); | ||
httpPromise.success(function(data, status, headers, config) { | ||
deferred.resolve(data); | ||
}) | ||
.error(function(data, status, headers, config) { | ||
deferred.reject({data: data, status: status}); | ||
}); | ||
|
||
return deferred.promise; | ||
}, | ||
reverseGeocode: function(latLng, bounds, region, restrictions, filters) { | ||
|
||
} | ||
}; | ||
return { | ||
name: 'angularGeoBing', | ||
config: function(config) { | ||
$$configuration.apiKey = config.apiKey; | ||
$$geoEndpoint = $$geoEndpoint.replace("{1}", $$configuration.apiKey); | ||
$$rgeoEndpoint = $$rgeoEndpoint.replace("{1}", $$configuration.apiKey); | ||
}, | ||
$get: function($log, $q, $http) { | ||
$$q = $q; | ||
$$log = $log; | ||
$$http = $http; | ||
return svc; | ||
} | ||
} | ||
}); | ||
}).call(this); |