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

Promisable methods #16

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Version
*/

var version = '0.2.1';
var version = '0.2.3';


/**
Expand Down Expand Up @@ -57,23 +57,22 @@ Geocoder.prototype = {
* @api public
*/

geocode: function ( loc, cbk, opts ) {
geocode: function ( loc, opts, cbk ) {

if ( ! loc ) {
return cbk( new Error( "Geocoder.geocode requires a location.") );
}

return this.providerObj.geocode(this.providerOpts, loc, cbk, opts);
return this.providerObj.geocode(this.providerOpts, loc, opts, cbk);

},

reverseGeocode: function ( lat, lng, cbk, opts ) {
reverseGeocode: function ( lat, lng, opts, cbk ) {
if ( !lat || !lng ) {
return cbk( new Error( "Geocoder.reverseGeocode requires a latitude and longitude." ) );
}

return this.providerObj.reverseGeocode(this.providerOpts, lat, lng, cbk, opts );

return this.providerObj.reverseGeocode(this.providerOpts, lat, lng, opts, cbk);
},

/**
Expand Down
22 changes: 13 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{
"name": "geocoder",
"description": "Geocoding through Google's Developer API",
"version": "0.2.2",
"main": "./index.js",
"description": "node wrapper around google's geocoder api",
"version": "0.2.4",
"main": "./index.js",
"author": "Stephen Wyatt Bush <[email protected]>",
"repository" : "git://github.com/wyattdanger/geocoder",
"homepage" : "https://github.com/wyattdanger/geocoder",
"keywords" : [ "google", "geocode", "geonames", "reverse geocode" ],
"repository": "git://github.com/wyattdanger/geocoder",
"homepage": "https://github.com/wyattdanger/geocoder",
"keywords": [
"google",
"geocode",
"geonames",
"reverse geocode"
],
"license": {
"type": "Apachev2",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
},
"dependencies" : {
"underscore" : "1.3.3",
"request":"2.11.1"
"dependencies": {
"extend": "^3.0.0",
"request": "2.11.1"
},
"optionalDependencies": {
"xml2js": "0.2.0"
Expand Down
14 changes: 7 additions & 7 deletions providers/geonames.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// xml2js is optional because only needed for geonames support
var xml2js = require("xml2js");
var request = require("request");
var _ = require('underscore');
var extend = require('extend');

exports.geocode = function ( providerOpts, loc, cbk, opts ) {
exports.geocode = function ( providerOpts, loc, opts, cbk ) {

var options = _.extend({q: loc, maxRows: 10, username:providerOpts.username||"demo" }, opts || {});
var options = extend({q: loc, maxRows: 10, username:providerOpts.username||"demo" }, opts || {});

request({
uri:"http://api.geonames.org/searchJSON",
Expand All @@ -24,9 +24,9 @@ exports.geocode = function ( providerOpts, loc, cbk, opts ) {
});
};

exports.reverseGeocode = function ( providerOpts, lat, lng, cbk, opts ) {
exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) {

var options = _.extend({lat:lat, lng:lng, username:providerOpts.username||"demo" }, opts || {});
var options = extend({lat:lat, lng:lng, username:providerOpts.username||"demo" }, opts || {});

request({
uri:"http://api.geonames.org/extendedFindNearby",
Expand Down Expand Up @@ -101,11 +101,11 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, cbk, opts ) {
"types":[ "country" ]
});

if (a.lat && typeof a.lat[0]=="string")
/*if (a.lat && typeof a.lat[0]=="string")
googlejson.results[0].geometry.location = {
"lat":parseFloat(a.lat[0]),
"lng":parseFloat(a.lng[0])
}
}*/
}

if (result.geonames.geoname) {
Expand Down
20 changes: 13 additions & 7 deletions providers/google.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var request = require("request");
var _ = require('underscore');
var extend = require('extend');

exports.geocode = function ( providerOpts, loc, cbk, opts ) {
exports.geocode = function ( providerOpts, loc, opts, cbk ) {

var options = _.extend({sensor: false, address: loc}, opts || {});
var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json"
var options = extend({address: loc}, opts || {});
var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json";
request({
uri: uri,
qs:options
Expand All @@ -21,10 +21,10 @@ exports.geocode = function ( providerOpts, loc, cbk, opts ) {
});
};

exports.reverseGeocode = function ( providerOpts, lat, lng, cbk, opts ) {
exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) {

var options = _.extend({sensor: false, latlng: lat + ',' + lng}, opts || {});
var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json"
var options = extend({latlng: lat + ',' + lng}, opts || {});
var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json";

request({
uri:uri,
Expand All @@ -38,6 +38,12 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, cbk, opts ) {
cbk(err);
return;
}

if(Array.isArray(result.results) && result.results.length > 0 && result.results[0].geometry && result.results[0].geometry.location) {
result.results[0].geometry.location.lat = parseFloat(lat);
result.results[0].geometry.location.lng = parseFloat(lng);
}

cbk(null,result);
});

Expand Down
144 changes: 144 additions & 0 deletions providers/here.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
var request = require("request");
var extend = require('extend');

exports.geocode = function ( providerOpts, loc, opts, cbk ) {

var options = extend({searchtext: loc, gen:"9", app_id:providerOpts.appid||"[yourappidhere]", app_code: providerOpts.appcode||"[yourappcodehere]" }, opts || {});

request({
uri:"http://geocoder.api.here.com/6.2/geocode.json",
qs:options
}, function(err,resp,body) {
if (err) return cbk(err);
var result;
try {
result = JSON.parse(body);
} catch (err) {
cbk(err);
return;
}
cbk(null,result);
});
};

// Here api https://developer.here.com/rest-apis/documentation/geocoder
exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) {

var options = extend({pos:lat+","+lng, mode:"trackPosition", gen:"9", app_id:providerOpts.appid||"[yourappidhere]", app_code: providerOpts.appcode||"[yourappcodehere]"}, opts || {});

request({
uri:"http://reverse.geocoder.api.here.com/6.2/reversegeocode.json",
qs:options,
headers: {
'User-Agent': 'request'
}
}, function(err,resp,body) {

// console.log("[GEOCODER Here API] uri:", "http://reverse.geocoder.api.here.com/6.2/reversegeocode.json");
// console.log("[GEOCODER Here API] options:", JSON.stringify(options));
// console.log("[GEOCODER Here API] body:", body);

if (err) return cbk(err);

var result;
try {
result = JSON.parse(body);
} catch (err) {
cbk(err);
return;
}

var view = result.Response.View[0];
if(!view || !view.Result || !Array.isArray(view.Result) || !view.Result[0] || !view.Result[0].Location) {
cbk(true, result);
return;
}

// Transform Here structure into something that looks like Google's JSON output
// https://developers.google.com/maps/documentation/geocoding/#JSON
var googlejson = {
"status":"OK",
"results":[
{
"address_components":[],
"formatted_address":"",
"geometry":{
"location":{
"lat":lat,
"lng":lng
}
}
}
]
};

var location = view.Result[0].Location;

if(location.Address) {

var a = location.Address;

var additionalData = {};

a.AdditionalData.map(function(obj) {
additionalData[obj.key] = obj.value;
});

if (a.HouseNumber || a.Building)
googlejson.results[0].address_components.push({
"long_name":a.HouseNumber || a.Building,
"short_name":a.HouseNumber || a.Building,
"types":["street_number"]
});

if (a.Street)
googlejson.results[0].address_components.push({
"long_name":a.Street,
"short_name":a.Street,
"types":["route"]
});

if (a.City || a.District)
googlejson.results[0].address_components.push({
"long_name": a.City || a.District,
"short_name": a.City || a.District,
"types":["locality", "political"]
});

if (a.State && typeof a.State=="string")
googlejson.results[0].address_components.push({
"long_name":additionalData.StateName || a.State,
"short_name":a.State,
"types":[ "administrative_area_level_1", "political" ]
});

if (a.County && typeof a.County=="string")
googlejson.results[0].address_components.push({
"long_name":additionalData.CountyName || a.County,
"short_name":a.County,
"types":[ "administrative_area_level_2", "political" ]
});

if (a.Country && typeof a.Country=="string")
googlejson.results[0].address_components.push({
"long_name":additionalData.CountryName,
"short_name":(additionalData.Country2 || a.Country.substring(0,2)).toUpperCase(),
"types":[ "country", "political" ]
});

/*if (location.DisplayPosition.Latitude && typeof location.DisplayPosition.Latitude=="string")
googlejson.results[0].geometry.location = {
"lat":parseFloat(location.DisplayPosition.Latitude),
"lng":parseFloat(location.DisplayPosition.Longitude)
}*/

// Make a formatted address as well as we can
googlejson.results[0].formatted_address = a.Label;
}

// console.log("[GEOCODER Here API], calling callback w/", JSON.stringify(googlejson));

cbk(null, googlejson);
});

};
Loading