Skip to content

Commit

Permalink
Merge pull request #2 from Swizec/detailed-geocode
Browse files Browse the repository at this point in the history
Detailed geocode
  • Loading branch information
feliperazeek committed Aug 18, 2011
2 parents 69642b9 + 9f6da34 commit c3d49a8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
34 changes: 19 additions & 15 deletions README.md
Expand Up @@ -14,44 +14,48 @@ Geo is a very basic, but simple, geo library for Node.js using Google's Geocode
## Usage - Geocode

var geo = require('geo');

var address = '885 6th Ave #15D New York, NY 10001';
var sensor = false;
geo.geocoder(geo.google, address, sensor, function(formattedAddress, latitude, longitude) {
geo.geocoder(geo.google, address, sensor,
function(formattedAddress, latitude, longitude, details) {
console.log("Formatted Address: " + formattedAddress);
console.log("Latitude: " + latitude);
console.log("Longitude: " + longitude);
console.log("Address details:", details);
});

// Reverse Geocoding also works
var latlong = { 'latitude': 52.5112, 'longitude': 13.45155};
geo.geocoder(geo.google, latlong, sensor, function(formattedAddress, latitude, longitude) {
geo.geocoder(geo.google, latlong, sensor,
function(formattedAddress, latitude, longitude, details) {
console.log("Formatted Address: " + formattedAddress);
console.log("Latitude: " + latitude);
console.log("Longitude: " + longitude);
console.log("Address details:", details);
});

## Usage - GeoHash

// First define a model instance
var model = {'address': '885 6th #15D, New York, NY 10001', 'baths': '1', 'beds': '1'};

// Define callback that gets the location (from a single field, multiple fields, whatever) from the model instance (model can be anything, DB class, JSON, array)
var locationGetterCallback = function(model) { return model['address']; };

// Define callback that will augment the model instance with geo information such as latitude, longitude and geohash
var geoSetterCallback = function(model, latitude, longitude, hash, callback) {
console.log('Geo Hash: ' + hash);
model['latitude'] = latitude;
model['longitude'] = longitude;
model['geohash'] = hash;
callback( model );
var geoSetterCallback = function(model, latitude, longitude, hash, callback) {
console.log('Geo Hash: ' + hash);
model['latitude'] = latitude;
model['longitude'] = longitude;
model['geohash'] = hash;
callback( model );
};

// Now let's see what happens with the model
geo.geomodel(model, locationGetterCallback, geoSetterCallback, function(model) {
console.log("Model: " + model['address'] + ', Geo: ' + model['geohash']);
});
});



Expand Down
40 changes: 26 additions & 14 deletions lib/geo.js
Expand Up @@ -11,7 +11,7 @@
this.version = [0, 0, 1];

// Import HTTP to Request Geocode
var http = require('http');
var http = require('http');
var geohash = require('geohash');

// Google Geocode Provider
Expand All @@ -37,18 +37,30 @@ GoogleGeocoder.prototype.request = function(location, sensor) {

// Google Geocode Provider - Response
GoogleGeocoder.prototype.responseHandler = function(response, callback) {
json = JSON.parse(response);
var json = JSON.parse(response);

var _details = function (data) {
var details = {};
data.address_components.map(function (d) {
d.types.map(function (k) {
details[k] = {long_name: d.long_name,
short_name: d.short_name};
});
});
return details;
};

if ( json.results == null || json.results[0] == null ) {
callback(null, null, null);
callback(null, null, null, null);
} else {
callback( json.results[0].formatted_address, json.results[0].geometry.location.lat, json.results[0].geometry.location.lng);
callback( json.results[0].formatted_address, json.results[0].geometry.location.lat, json.results[0].geometry.location.lng, _details(json.results[0]));
}
};


// Main Geo Class
function Geo() {

// Read more about it: http://ejohn.org/blog/adv-javascript-and-processingjs/
if (! (this instanceof arguments.callee)) {
return new arguments.callee(arguments);
Expand All @@ -57,15 +69,15 @@ function Geo() {
// Init Class
var self = this;
self.init();

};

// Constructor
Geo.prototype.init = function() {

var self = this;
self.google = new GoogleGeocoder();

};

// Get Geocode
Expand All @@ -83,28 +95,28 @@ Geo.prototype.geocoder = function(geocoder, location, sensor, callback) {
if ( callback == null ) {
throw new Error("Invalid Callback");
}

// Get HTTP Request Options
var options = geocoder.request(location, sensor);

// Make Request
connection = http.createClient(options.port, options.host);
var connection = http.createClient(options.port, options.host);
var request = connection.request("GET", options.path);

// Handle Response
request.addListener("response", function (response) {
var responseBody = "";
var responseBody = "";
response.addListener("data", function(chunk) {
responseBody += chunk;
});
response.addListener("end", function() {
geocoder.responseHandler(responseBody, callback);
});
});

// End Request - Listener will take care of Response
request.end();

};

// Get GeoHash
Expand Down

0 comments on commit c3d49a8

Please sign in to comment.