Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Convert Method #14

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log
.DS_Store
ENV_VARS

public/fb_images/*
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ To set the configuration you call `gm.config(key, value)` or `gm.config({key: va

`google-private-key`- used for setting business specific parameters

# Utilities

Retrieving a JSON blob from google maps can sometimes be a bit messy if you're trying to convert data on the fly. You can use the convert method to change the blob into a goeocode, city, zip, county, state, or country.

gm.geocode('64111', function(err,data){
gm.utils.convert(data, 'state', function(result){
util.puts(JSON.stringify(data));
});
});

-------------

All the googlemaps functions follow this scheme:
Expand Down
63 changes: 63 additions & 0 deletions lib/googlemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ exports.setProxy = function(uri) {
config('proxy', uri);
};

exports.utils = {}

var _config = {
'google-client-id': null,
'stagger-time': 200,
Expand Down Expand Up @@ -350,6 +352,67 @@ exports.checkAndConvertPoint = function(input) {
throw(new Error("Unrecognized input: checkAndConvertPoint accepts Arrays of Numbers and Strings"));
};

exports.utils.convert = function(data, location, callback) {

address_blob = data.results[0].address_components
full_address = {
city : 'Could not find city',
county : 'Could not find county',
state : 'Could not find state',
country : 'Could not find country',
zip : 'Could not find zip',
geo : 'Could not find geo'
}

if(data.results[0].geometry.location){
full_address.geo = data.results[0].geometry.location
}

var component, _i, _len;

for (_i = 0, _len = address_blob.length; _i < _len; _i++) {
component = address_blob[_i];
if (component.types[0] === 'locality') {
full_address.city = component.long_name;
} else if (component.types[0] === 'administrative_area_level_1') {
full_address.state = component.long_name;
} else if (component.types[0] === 'administrative_area_level_2') {
full_address.county = component.long_name;
} else if (component.types[0] === 'country') {
full_address.country = component.long_name;
} else if (component.types[0] === 'postal_code') {
full_address.zip = component.long_name;
}
}

switch (location) {
case 'city' :
callback(full_address.city);
break;
case 'county' :
callback(full_address.county);
break;
case 'zip' :
callback(full_address.zip);
break;
case 'state' :
callback(full_address.state);
break;
case 'country':
callback(full_address.country);
break;
case 'geo' :
callback(full_address.geo);
break;
case 'all' :
callback(full_address);
break;
default :
callback(full_address);
break;
}
}

// Wraps the callback function to convert the output to a javascript object
var returnObjectFromJSON = function(callback) {
if (typeof callback === 'function') {
Expand Down
41 changes: 41 additions & 0 deletions test/conversion-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var vows = require('vows'),
assert = require('assert'),
gm = require('../lib/googlemaps');

vows.describe('geocode').addBatch({
'Geocode Conversion (Kansas City)': {
topic: function(){
gm.geocode('64111', this.callback, 'false');
},
'can convert zip to state': function(err, result){
gm.utils.convert(result, 'state', function(data){
assert.equal(data , 'Missouri');
})
},
'can convert zip to country': function(err, result){
gm.utils.convert(result, 'country', function(data){
assert.equal(data , 'United States');
})
},
'can convert zip to geo': function(err, result){
gm.utils.convert(result, 'geo', function(data){
assert.equal(data.lat , '39.0587452');
assert.equal(data.lng , '-94.5985613');
})
}
}
}).export(module);


vows.describe('geocode').addBatch({
'ReverseGeocode Conversion (Kansas City)': {
topic: function(){
gm.reverseGeocode('39.0587452,-94.5985613', this.callback, 'false');
},
'can convert geo to state': function(err, result){
gm.utils.convert(result, 'state', function(data){
assert.equal(data , 'Missouri');
})
}
}
}).export(module);
2 changes: 2 additions & 0 deletions test/geocode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ vows.describe('geocode').addBatch({
}).export(module);




/* Geocode query results
{
"status":"OK",
Expand Down