Permalink
Browse files

added support for reverse geocoding

  • Loading branch information...
1 parent 87b0888 commit 748f4e672cfdefd71532bae5d4cd47c4dc38be04 Stephan Eckardt committed Jul 2, 2011
Showing with 46 additions and 2 deletions.
  1. +7 −1 README.md
  2. +30 −0 examples/simple_reverse_geocode_lookup.js
  3. +9 −1 lib/geo.js
View
@@ -23,7 +23,13 @@ Geo is a very basic, but simple, geo library for Node.js using Google's Geocode
console.log("Longitude: " + longitude);
});
-
+ // Reverse Geocoding also works
+ var latlong = { 'latitude': 52.5112, 'longitude': 13.45155};
+ geo.geocoder(geo.google, latlong, sensor, function(formattedAddress, latitude, longitude) {
+ console.log("Formatted Address: " + formattedAddress);
+ console.log("Latitude: " + latitude);
+ console.log("Longitude: " + longitude);
+ });
## Usage - GeoHash
@@ -0,0 +1,30 @@
+// *********************************************
+// *********************************************
+// Geo by Felipe Oliveira
+// March 2011
+// http://geeks.aretotally.in/projects/geo
+// http://twitter.com/_felipera
+// *********************************************
+// *********************************************
+
+var geo = require('../lib/geo');
+
+// Reverse Geocoding also works
+var latlong = { 'latitude': 52.5112, 'longitude': 13.45155};
+geo.geocoder(geo.google, latlong, sensor, function(formattedAddress, latitude, longitude) {
+ console.log("Formatted Address: " + formattedAddress);
+ console.log("Latitude: " + latitude);
+ console.log("Longitude: " + longitude);
+});
+
+
+// Bad Address
+// var address = '1111 Foobar Ave #15D New York, NY 9999';
+// var sensor = false;
+// geo.geocoder(geo.google, address, sensor, function(formattedAddress, latitude, longitude) {
+ // console.log("Formatted Address: " + formattedAddress);
+ // console.log("Latitude: " + latitude);
+ // console.log("Longitude: " + longitude);
+// });
+
+
View
@@ -19,10 +19,18 @@ function GoogleGeocoder() {};
// Google Geocode Provider - Request
GoogleGeocoder.prototype.request = function(location, sensor) {
+ if (typeof location == 'object' && location['latitude'] != null && location['longitude'] != null) {
+ // if location is an object with lat and long properties, we do reverse geocoding
+ var path = '/maps/api/geocode/json?latlng=' + escape(location['latitude'].toString() + ',' + location['longitude'].toString()) + '&sensor=' + sensor;
+ } else {
+ // normal forward geocoding
+ var path = '/maps/api/geocode/json?address=' + escape(location) + '&sensor=' + sensor;
+ }
+
return {
host: 'maps.googleapis.com',
port: 80,
- path: '/maps/api/geocode/json?address=' + escape(location) + '&sensor=' + sensor,
+ path: path,
method: 'GET'
};
};

0 comments on commit 748f4e6

Please sign in to comment.