Browse files

Add reverseGeo to google provider, Fix getCurrentPosition reverseGeo bug

  • Loading branch information...
1 parent caeae92 commit ae2e51ef3a14d02de290cc9d5485c93173527143 @mikepugh committed Feb 3, 2014
Showing with 88 additions and 45 deletions.
  1. +24 −2 scripts/angulargeo.js
  2. +54 −41 scripts/providers/angulargeo-google.js
  3. +8 −0 test/app.js
  4. +2 −2 test/index.html
View
26 scripts/angulargeo.js
@@ -53,6 +53,7 @@
}, function(err) {
if($$providers.length === 1) {
deferred.reject(err);
+ return;
}
if($$currentProvider < $$providers.length - 1) {
$$currentProvider++;
@@ -64,6 +65,26 @@
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);
@@ -77,14 +98,15 @@
var deferred = $q.defer();
$window.navigator.geolocation.getCurrentPosition(function(pos) {
if(autoReverseGeocode) {
- var p = self.reverseGeocode({latitude: pos.coords.latitude, longitude: pos.coords.longitude}, null, null, null, null);
+ 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);
}
- deferred.resolve(pos);
}, function(err) {
deferred.reject(err);
}, options);
View
95 scripts/providers/angulargeo-google.js
@@ -2,48 +2,61 @@
* Created by Mike on 1/31/14.
*/
(function() {
- "use strict";
- angular.module('angular-geo-providers.google', [])
- .provider('angularGeoGoogle', function() {
- var $$q, $$log;
- var $$configuration;
+ "use strict";
+ angular.module('angular-geo-providers.google', [])
+ .provider('angularGeoGoogle', function() {
+ var $$q, $$log;
+ var $$configuration;
- if(typeof google === 'undefined' || (typeof google !== 'undefined' && typeof google.maps === 'undefined')) {
- throw new Error("Google Maps API V3 is required for angulargeo-google to function, please include it");
+ if(typeof google === 'undefined' || (typeof google !== 'undefined' && typeof google.maps === 'undefined')) {
+ throw new Error("Google Maps API V3 is required for angulargeo-google to function, please include it");
+ }
+ var $$geocoder = new google.maps.Geocoder();
+ var svc = {
+ geocode: function(address, bounds, region, restrictions, filters) {
+ var deferred = $$q.defer();
+ $$geocoder.geocode({'address': address}, function(results, status) {
+ if(status === google.maps.GeocoderStatus.OK) {
+ //todo: Normalize result set once we have a better idea what bing, etc apis return
+ deferred.resolve(results);
+ } else if(status === google.maps.GeocoderStatus.ZERO_RESULTS) {
+ deferred.resolve(null);
+ } else if(status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
+ deferred.reject("OVER_LIMIT")
+ } else if(status === google.maps.GeocoderStatus.REQUEST_DENIED || status === google.maps.GeocoderStatus.INVALID_REQUEST) {
+ deferred.reject("FAILED");
}
- var $$geocoder = new google.maps.Geocoder();
- var svc = {
- geocode: function(address, bounds, region, restrictions, filters) {
- var deferred = $$q.defer();
- $$geocoder.geocode({'address': address}, function(results, status) {
- if(status === google.maps.GeocoderStatus.OK) {
- //todo: Normalize result set once we have a better idea what bing, etc apis return
- deferred.resolve(results);
- } else if(status === google.maps.GeocoderStatus.ZERO_RESULTS) {
- deferred.resolve(null);
- } else if(status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
- deferred.reject("OVER_LIMIT")
- } else if(status === google.maps.GeocoderStatus.REQUEST_DENIED || status === google.maps.GeocoderStatus.INVALID_REQUEST) {
- deferred.reject("FAILED");
- }
- });
- return deferred.promise;
- },
- reverseGeocode: function(latLng, bounds, region, restrictions, filters) {
-
- }
- };
- return {
- name: 'angularGeoGoogle',
- config: function(config) {
- // do some config? set api-key?
-
- },
- $get: function($log, $q) {
- $$q = $q;
- $$log = $log;
- return svc;
- }
+ });
+ return deferred.promise;
+ },
+ reverseGeocode: function(latLng, bounds, region, restrictions, filters) {
+ var deferred = $$q.defer();
+ $$geocoder.geocode({'latLng': new google.maps.LatLng(latLng.latitude, latLng.longitude)}, function(results, status) {
+ if(status === google.maps.GeocoderStatus.OK) {
+ //todo: Normalize result set once we have a better idea what bing, etc apis return
+ deferred.resolve(results);
+ } else if(status === google.maps.GeocoderStatus.ZERO_RESULTS) {
+ deferred.resolve(null);
+ } else if(status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
+ deferred.reject("OVER_LIMIT")
+ } else if(status === google.maps.GeocoderStatus.REQUEST_DENIED || status === google.maps.GeocoderStatus.INVALID_REQUEST) {
+ deferred.reject("FAILED");
}
- });
+ });
+ return deferred.promise;
+ }
+ };
+ return {
+ name: 'angularGeoGoogle',
+ config: function(config) {
+ // do some config? set api-key?
+
+ },
+ $get: function($log, $q) {
+ $$q = $q;
+ $$log = $log;
+ return svc;
+ }
+ }
+ });
}).call(this);
View
8 test/app.js
@@ -23,5 +23,13 @@
$log.error(err);
});
}
+ $scope.getLocation = function() {
+ angularGeo.getCurrentPosition({},true).then(function(results) {
+ $scope.geo = results;
+ }, function(err) {
+ $log.error(err);
+ })
+ }
+
});
}).call(this);
View
4 test/index.html
@@ -7,7 +7,7 @@
<link rel="stylesheet" href="css/style/semantic.css"/>
<!--<script src="//ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>-->
<script src='//maps.googleapis.com/maps/api/js?sensor=false'></script>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
+ <script src="../bower_components/angular/angular.min.js"></script>
<script src="../scripts/providers/angulargeo-google.js"></script>
<script src="../scripts/providers/angulargeo-bing.js"></script>
<script src="../scripts/angulargeo.js"></script>
@@ -24,7 +24,7 @@
<div class="ui buttons">
<button class="ui button" data-ng-click="geocodeAddress()">Geo</button>
<div class="or"></div>
- <button class="ui position button">Semantic UI Testing!</button>
+ <button class="ui position button" data-ng-click="getLocation()">Get Current Location</button>
</div>
</div>

0 comments on commit ae2e51e

Please sign in to comment.