From 26d4430d0be8684ca27d2428c79cd42d20f53b0f Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Wed, 21 Oct 2015 18:09:53 +0530 Subject: [PATCH 01/18] sensor parameter is not required by Google --- providers/google.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/providers/google.js b/providers/google.js index 80f8931..d7af7be 100644 --- a/providers/google.js +++ b/providers/google.js @@ -3,7 +3,7 @@ var _ = require('underscore'); exports.geocode = function ( providerOpts, loc, cbk, opts ) { - var options = _.extend({sensor: false, address: loc}, opts || {}); + var options = _.extend({address: loc}, opts || {}); var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json" request({ uri: uri, @@ -23,7 +23,7 @@ exports.geocode = function ( providerOpts, loc, cbk, opts ) { exports.reverseGeocode = function ( providerOpts, lat, lng, cbk, opts ) { - var options = _.extend({sensor: false, latlng: lat + ',' + lng}, opts || {}); + var options = _.extend({latlng: lat + ',' + lng}, opts || {}); var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json" request({ From 8ddf59de53f7b561f46af5b847465a7151361c38 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Wed, 21 Oct 2015 18:17:01 +0530 Subject: [PATCH 02/18] remove underscore dependency, use extend instead --- providers/geonames.js | 6 +++--- providers/google.js | 6 +++--- providers/yahoo.js | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/providers/geonames.js b/providers/geonames.js index 6336fd1..a1770fd 100644 --- a/providers/geonames.js +++ b/providers/geonames.js @@ -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 ) { - 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", @@ -26,7 +26,7 @@ exports.geocode = function ( providerOpts, loc, cbk, opts ) { exports.reverseGeocode = function ( providerOpts, lat, lng, cbk, opts ) { - 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", diff --git a/providers/google.js b/providers/google.js index d7af7be..84ab11c 100644 --- a/providers/google.js +++ b/providers/google.js @@ -1,9 +1,9 @@ var request = require("request"); -var _ = require('underscore'); +var extend = require('extend'); exports.geocode = function ( providerOpts, loc, cbk, opts ) { - var options = _.extend({address: loc}, opts || {}); + var options = extend({address: loc}, opts || {}); var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json" request({ uri: uri, @@ -23,7 +23,7 @@ exports.geocode = function ( providerOpts, loc, cbk, opts ) { exports.reverseGeocode = function ( providerOpts, lat, lng, cbk, opts ) { - var options = _.extend({latlng: lat + ',' + lng}, opts || {}); + var options = extend({latlng: lat + ',' + lng}, opts || {}); var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json" request({ diff --git a/providers/yahoo.js b/providers/yahoo.js index 639af4f..07c43cd 100644 --- a/providers/yahoo.js +++ b/providers/yahoo.js @@ -1,11 +1,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 ) { - var options = _.extend({q: loc, flags: "J", appid:providerOpts.appid||"[yourappidhere]" }, opts || {}); + var options = extend({q: loc, flags: "J", appid:providerOpts.appid||"[yourappidhere]" }, opts || {}); request({ uri:"http://where.yahooapis.com/geocode", @@ -26,7 +26,7 @@ exports.geocode = function ( providerOpts, loc, cbk, opts ) { // yahoo placefinder api http://developer.yahoo.com/geo/placefinder/guide/ exports.reverseGeocode = function ( providerOpts, lat, lng, cbk, opts ) { - var options = _.extend({q: lat+", "+lng, gflags:"R", flags: "J", appid:providerOpts.appid||"[yourappidhere]" }, opts || {}); + var options = extend({q: lat+", "+lng, gflags:"R", flags: "J", appid:providerOpts.appid||"[yourappidhere]" }, opts || {}); request({ uri:"http://where.yahooapis.com/geocode", From 05175d55a12add37b669edc917a59965e906662f Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Wed, 21 Oct 2015 19:48:18 +0530 Subject: [PATCH 03/18] save dependencies --- package.json | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 2b4a34f..45ceb05 100644 --- a/package.json +++ b/package.json @@ -1,20 +1,24 @@ { "name": "geocoder", - "description": "Geocoding through Google's Developer API", + "description": "node wrapper around google's geocoder api", "version": "0.2.2", "main": "./index.js", - "description": "node wrapper around google's geocoder api", "author": "Stephen Wyatt Bush ", - "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" From aea4a25af5a772c005bb0342a36efb94738d9594 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Thu, 22 Oct 2015 14:40:19 +0530 Subject: [PATCH 04/18] reorder method params node.js style - make promisable --- index.js | 7 +++---- providers/geonames.js | 4 ++-- providers/google.js | 6 +++--- providers/yahoo.js | 4 ++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index c86efa6..c3e72fb 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,7 @@ 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.") ); @@ -67,13 +67,12 @@ Geocoder.prototype = { }, - 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); }, /** diff --git a/providers/geonames.js b/providers/geonames.js index a1770fd..8ff6ac6 100644 --- a/providers/geonames.js +++ b/providers/geonames.js @@ -4,7 +4,7 @@ var xml2js = require("xml2js"); var request = require("request"); 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 || {}); @@ -24,7 +24,7 @@ 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 || {}); diff --git a/providers/google.js b/providers/google.js index 84ab11c..e623540 100644 --- a/providers/google.js +++ b/providers/google.js @@ -1,7 +1,7 @@ var request = require("request"); var extend = require('extend'); -exports.geocode = function ( providerOpts, loc, cbk, opts ) { +exports.geocode = function ( providerOpts, loc, opts, cbk ) { var options = extend({address: loc}, opts || {}); var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json" @@ -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({latlng: lat + ',' + lng}, opts || {}); - var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json" + var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json"; request({ uri:uri, diff --git a/providers/yahoo.js b/providers/yahoo.js index 07c43cd..e9fb7eb 100644 --- a/providers/yahoo.js +++ b/providers/yahoo.js @@ -3,7 +3,7 @@ var xml2js = require("xml2js"); var request = require("request"); var extend = require('extend'); -exports.geocode = function ( providerOpts, loc, cbk, opts ) { +exports.geocode = function ( providerOpts, loc, opts, cbk ) { var options = extend({q: loc, flags: "J", appid:providerOpts.appid||"[yourappidhere]" }, opts || {}); @@ -24,7 +24,7 @@ exports.geocode = function ( providerOpts, loc, cbk, opts ) { }; // yahoo placefinder api http://developer.yahoo.com/geo/placefinder/guide/ -exports.reverseGeocode = function ( providerOpts, lat, lng, cbk, opts ) { +exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { var options = extend({q: lat+", "+lng, gflags:"R", flags: "J", appid:providerOpts.appid||"[yourappidhere]" }, opts || {}); From 83874a7d2d418051f0872f0cf65b70a340e0647b Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Thu, 22 Oct 2015 15:13:07 +0530 Subject: [PATCH 05/18] bump version to 0.2.3 --- index.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c3e72fb..9e2eb2c 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ * Version */ -var version = '0.2.1'; +var version = '0.2.3'; /** diff --git a/package.json b/package.json index 45ceb05..d25b50d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "geocoder", "description": "node wrapper around google's geocoder api", - "version": "0.2.2", + "version": "0.2.3", "main": "./index.js", "author": "Stephen Wyatt Bush ", "repository": "git://github.com/wyattdanger/geocoder", From 87f8ff165b47d21831ecdf09ed3bb9b57dec9c63 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Thu, 22 Oct 2015 15:16:20 +0530 Subject: [PATCH 06/18] update test case params --- test/geocoder-google-test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/geocoder-google-test.js b/test/geocoder-google-test.js index bae38fb..d6593af 100644 --- a/test/geocoder-google-test.js +++ b/test/geocoder-google-test.js @@ -17,7 +17,7 @@ module.exports = { testGeocode: function(test){ test.expect(3); - geocoder.geocode("Munich, Germany", function(err, result){ + geocoder.geocode("Munich, Germany", {}, function(err, result){ test.ok(!err); test.equals('OK', result.status); test.ok(result.results[0].formatted_address.match(/Munich/)); @@ -27,7 +27,7 @@ module.exports = { testReverseGeocode: function(test){ test.expect(7); - geocoder.reverseGeocode(49.101,6.1442, function(err, result){ + geocoder.reverseGeocode(49.101,6.1442, {}, function(err, result){ test.ok(!err); test.equals('OK', result.status); // console.error(result.results[0].formatted_address); @@ -50,7 +50,7 @@ module.exports = { testReverseGeocodeGoogleplex: function(test){ test.expect(9); - geocoder.reverseGeocode(37.42291810, -122.08542120, function(err, result){ + geocoder.reverseGeocode(37.42291810, -122.08542120, {}, function(err, result){ test.ok(!err); test.equals('OK', result.status); test.ok(result.results[0].formatted_address.match(/Mountain View/i)); @@ -76,7 +76,7 @@ module.exports = { testLanguage: function(test){ test.expect(3); - geocoder.geocode("Plattlinger Str. 10, 81479 München, Deutschland", function(err, result){ + geocoder.geocode("Plattlinger Str. 10, 81479 München, Deutschland", {}, function(err, result){ test.ok(!err); test.equals('OK', result.status); test.ok(result.results[0].formatted_address.match(/München/)); From 670c4402bc56f0900e4ff72831772d6ef90e2c70 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Thu, 22 Oct 2015 15:29:42 +0530 Subject: [PATCH 07/18] move params --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 9e2eb2c..ece135f 100644 --- a/index.js +++ b/index.js @@ -63,7 +63,7 @@ Geocoder.prototype = { 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); }, From 8772f1564cd56e5191e104a14eb281d2a921acae Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Thu, 22 Oct 2015 15:29:53 +0530 Subject: [PATCH 08/18] add missing semicolon --- providers/google.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/google.js b/providers/google.js index e623540..4561d7d 100644 --- a/providers/google.js +++ b/providers/google.js @@ -4,7 +4,7 @@ var extend = require('extend'); exports.geocode = function ( providerOpts, loc, opts, cbk ) { var options = extend({address: loc}, opts || {}); - var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json" + var uri = "http" + ( options.key ? "s" : "" ) + "://maps.googleapis.com/maps/api/geocode/json"; request({ uri: uri, qs:options From 1a05f7a090c57c15de32e68245eb9d4aed02728d Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Thu, 31 Dec 2015 11:46:31 +0530 Subject: [PATCH 09/18] do not include xml2js in yahoo geocoder --- providers/yahoo.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/providers/yahoo.js b/providers/yahoo.js index e9fb7eb..b241d7b 100644 --- a/providers/yahoo.js +++ b/providers/yahoo.js @@ -1,5 +1,3 @@ -// xml2js is optional because only needed for geonames support -var xml2js = require("xml2js"); var request = require("request"); var extend = require('extend'); From d62cbfd2d9bd59522deb01a531de8521c86db19e Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Mon, 4 Jan 2016 16:31:15 +0530 Subject: [PATCH 10/18] add nominatim api and tests --- providers/nominatim.js | 133 +++++++++++++++++++++++++++++++++++++ test/geocoder-nominatim.js | 28 ++++++++ 2 files changed, 161 insertions(+) create mode 100644 providers/nominatim.js create mode 100644 test/geocoder-nominatim.js diff --git a/providers/nominatim.js b/providers/nominatim.js new file mode 100644 index 0000000..267e024 --- /dev/null +++ b/providers/nominatim.js @@ -0,0 +1,133 @@ +var request = require("request"); +var extend = require('extend'); + +exports.geocode = function ( providerOpts, loc, opts, cbk ) { + + var options = extend({q: loc, format: "json", addressdetails:"1" }, opts || {}); + + request({ + uri:"http://nominatim.openstreetmap.org/search", + 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); + }); +}; + +// Nominatim api http://wiki.openstreetmap.org/wiki/Nominatim +exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { + + var options = extend({lat:lat, lon:lng, format:"json", addressdetails:"1"}, opts || {}); + + request({ + uri:"http://nominatim.openstreetmap.org/reverse", + qs:options, + headers: { + 'User-Agent': 'request' + } + }, function(err,resp,body) { + + // console.log("[GEOCODER Nominatim API] uri:", "http://nominatim.openstreetmap.org/reverse"); + // console.log("[GEOCODER Nominatim API] options:", JSON.stringify(options)); + // console.log("[GEOCODER Nominatim API] body:", body); + + if (err) return cbk(err); + + var result; + try { + result = JSON.parse(body); + } catch (err) { + cbk(err); + return; + } + + // Transform Nominatim 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 + } + }, + "place_id": "" + } + ] + }; + + if(result.address) { + var a = result.address; + + if (a.house_number || a.building) + googlejson.results[0].address_components.push({ + "long_name":a.house_number || a.building, + "short_name":a.house_number || a.building, + "types":["street_number"] + }); + + if (a.road || a.cycleway) + googlejson.results[0].address_components.push({ + "long_name":a.road || a.cycleway, + "short_name":a.road || a.cycleway, + "types":["route"] + }); + + if (a.city || a.town || a.village || e.hamlet) + googlejson.results[0].address_components.push({ + "long_name": a.city || a.town || a.village || e.hamlet, + "short_name": a.city || a.town || a.village || e.hamlet, + "types":["locality", "political"] + }); + + if (a.state && typeof a.state=="string") + googlejson.results[0].address_components.push({ + "long_name":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":a.county, + "short_name":a.county, + "types":[ "administrative_area_level_2", "political" ] + }); + + if (a.country && a.country_code && typeof a.country=="string") + googlejson.results[0].address_components.push({ + "long_name":a.country, + "short_name":a.country_code.toUpperCase(), + "types":[ "country", "political" ] + }); + + if (result.lat && typeof a.lat=="string") + googlejson.results[0].geometry.location = { + "lat":parseFloat(result.lat), + "lng":parseFloat(result.lon) + } + } + + // Make a formatted address as well as we can + googlejson.results[0].formatted_address = result.display_name; + + // Set place id + googlejson.results[0].place_id = result.place_id; + + // console.log("[GEOCODER Nominatim API], calling callback w/", JSON.stringify(googlejson)); + + cbk(null, googlejson); + }); + +}; diff --git a/test/geocoder-nominatim.js b/test/geocoder-nominatim.js new file mode 100644 index 0000000..83eeabf --- /dev/null +++ b/test/geocoder-nominatim.js @@ -0,0 +1,28 @@ +geocoder = require('../index.js'); + + + +module.exports = { + + setUp:function(cb) { + geocoder.selectProvider("nominatim",{"username":"npmunittests"}); + cb(); + }, + + testExposeGeocodeFunction: function(test){ + test.equal(typeof geocoder.geocode, 'function'); + test.equal(geocoder.provider, 'nominatim'); + test.done() + }, + + // Uses "nominatim" + testReverseGeocode: function(test){ + return require('./geocoder-google-test.js').testReverseGeocode(test); + }, + + // Uses "address" + /*testReverseGeocodeGoogleplex: function(test){ + return require('./geocoder-google-test.js').testReverseGeocodeGoogleplex(test); + }, +*/ +} From de57c7c30b267eed478a3e09b89ef187027f60f4 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Tue, 5 Jan 2016 14:26:00 +0530 Subject: [PATCH 11/18] add here api and tests --- providers/here.js | 144 ++++++++++++++++++++++++++++++++++++++++++ test/geocoder-here.js | 28 ++++++++ 2 files changed, 172 insertions(+) create mode 100644 providers/here.js create mode 100644 test/geocoder-here.js diff --git a/providers/here.js b/providers/here.js new file mode 100644 index 0000000..d620a3e --- /dev/null +++ b/providers/here.js @@ -0,0 +1,144 @@ +var request = require("request"); +var extend = require('extend'); + +exports.geocode = function ( providerOpts, loc, opts, cbk ) { + + console.log("Here"); + + 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) { + cbk(false, result); + } + + // 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); + }); + +}; diff --git a/test/geocoder-here.js b/test/geocoder-here.js new file mode 100644 index 0000000..5ac2fdc --- /dev/null +++ b/test/geocoder-here.js @@ -0,0 +1,28 @@ +geocoder = require('../index.js'); + + + +module.exports = { + + setUp:function(cb) { + geocoder.selectProvider("here",{"appid": "sS9YNh3wSKTMMDlPvpdW", "appcode": "-junJXzl1ASWYAhDvLCfeg"}); + cb() + }, + + testExposeGeocodeFunction: function(test){ + test.equal(typeof geocoder.geocode, 'function'); + test.equal(geocoder.provider, 'here'); + test.done() + }, + + // Uses "here" + testReverseGeocode: function(test){ + return require('./geocoder-google-test.js').testReverseGeocode(test); + }, + + // Uses "address" + testReverseGeocodeGoogleplex: function(test){ + return require('./geocoder-google-test.js').testReverseGeocodeGoogleplex(test); + }, + +} From 34f8dcf32d482e42f2cd41edcff88d15f3174fa3 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Tue, 5 Jan 2016 14:27:44 +0530 Subject: [PATCH 12/18] bump to 0.2.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d25b50d..3555c0c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "geocoder", "description": "node wrapper around google's geocoder api", - "version": "0.2.3", + "version": "0.2.4", "main": "./index.js", "author": "Stephen Wyatt Bush ", "repository": "git://github.com/wyattdanger/geocoder", From 2a32a4f8186ba5571bb142a235ddf660ffabc4ce Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Wed, 6 Jan 2016 21:33:27 +0530 Subject: [PATCH 13/18] callback with error if no view is found --- providers/here.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/here.js b/providers/here.js index d620a3e..5c08520 100644 --- a/providers/here.js +++ b/providers/here.js @@ -52,7 +52,7 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { var view = result.Response.View[0]; if(!view) { - cbk(false, result); + cbk(true, result); } // Transform Here structure into something that looks like Google's JSON output From 4c62c2ffa1a8c8cebb6c96a241daef9e9e891f44 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Wed, 6 Jan 2016 21:34:19 +0530 Subject: [PATCH 14/18] fix typo --- providers/nominatim.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/providers/nominatim.js b/providers/nominatim.js index 267e024..48cfad1 100644 --- a/providers/nominatim.js +++ b/providers/nominatim.js @@ -84,10 +84,10 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { "types":["route"] }); - if (a.city || a.town || a.village || e.hamlet) + if (a.city || a.town || a.village || a.hamlet) googlejson.results[0].address_components.push({ - "long_name": a.city || a.town || a.village || e.hamlet, - "short_name": a.city || a.town || a.village || e.hamlet, + "long_name": a.city || a.town || a.village || a.hamlet, + "short_name": a.city || a.town || a.village || a.hamlet, "types":["locality", "political"] }); From 8288943346e35dea1be557243163d83c29467dcb Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Thu, 7 Jan 2016 18:43:33 +0530 Subject: [PATCH 15/18] do not continue if no view is found --- providers/here.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/providers/here.js b/providers/here.js index 5c08520..e9866d5 100644 --- a/providers/here.js +++ b/providers/here.js @@ -3,8 +3,6 @@ var extend = require('extend'); exports.geocode = function ( providerOpts, loc, opts, cbk ) { - console.log("Here"); - var options = extend({searchtext: loc, gen:"9", app_id:providerOpts.appid||"[yourappidhere]", app_code: providerOpts.appcode||"[yourappcodehere]" }, opts || {}); request({ @@ -53,6 +51,7 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { var view = result.Response.View[0]; if(!view) { cbk(true, result); + return; } // Transform Here structure into something that looks like Google's JSON output From aa96cc682d875d690197eb28602502e1bc8db3d7 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Thu, 7 Jan 2016 18:55:49 +0530 Subject: [PATCH 16/18] return original lat/lng after reverse geocode --- providers/geonames.js | 4 ++-- providers/google.js | 4 ++++ providers/here.js | 4 ++-- providers/nominatim.js | 4 ++-- providers/yahoo.js | 4 ++-- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/providers/geonames.js b/providers/geonames.js index 8ff6ac6..5ab604f 100644 --- a/providers/geonames.js +++ b/providers/geonames.js @@ -101,11 +101,11 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { "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) { diff --git a/providers/google.js b/providers/google.js index 4561d7d..9a78104 100644 --- a/providers/google.js +++ b/providers/google.js @@ -38,6 +38,10 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { cbk(err); return; } + + result.results[0].geometry.location.lat = parseFloat(lat); + result.results[0].geometry.location.lng = parseFloat(lng); + cbk(null,result); }); diff --git a/providers/here.js b/providers/here.js index e9866d5..b02db77 100644 --- a/providers/here.js +++ b/providers/here.js @@ -125,11 +125,11 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { "types":[ "country", "political" ] }); - if (location.DisplayPosition.Latitude && typeof location.DisplayPosition.Latitude=="string") + /*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; diff --git a/providers/nominatim.js b/providers/nominatim.js index 48cfad1..2574937 100644 --- a/providers/nominatim.js +++ b/providers/nominatim.js @@ -112,11 +112,11 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { "types":[ "country", "political" ] }); - if (result.lat && typeof a.lat=="string") + /*if (result.lat && typeof a.lat=="string") googlejson.results[0].geometry.location = { "lat":parseFloat(result.lat), "lng":parseFloat(result.lon) - } + }*/ } // Make a formatted address as well as we can diff --git a/providers/yahoo.js b/providers/yahoo.js index b241d7b..796b5f3 100644 --- a/providers/yahoo.js +++ b/providers/yahoo.js @@ -131,11 +131,11 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { "types":[ "postal_code" ] }); - if (a.latitude) + /*if (a.latitude) googlejson.results[0].geometry.location = { "lat":parseFloat(a.latitude), "lng":parseFloat(a.longitude) - }; + };*/ // Make a formatted address as well as we can var formatted = []; From 7d5520becab56137ea5b4a87ad7f910cd58260c2 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Thu, 7 Jan 2016 19:05:22 +0530 Subject: [PATCH 17/18] check if results are available --- providers/google.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/providers/google.js b/providers/google.js index 9a78104..85455a2 100644 --- a/providers/google.js +++ b/providers/google.js @@ -39,8 +39,10 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { return; } - result.results[0].geometry.location.lat = parseFloat(lat); - result.results[0].geometry.location.lng = parseFloat(lng); + 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); }); From 7ae364f8496f3a63a4bdb1ad15ef2c2349607b95 Mon Sep 17 00:00:00 2001 From: Tushar Agarwal Date: Wed, 15 Jun 2016 16:09:09 +0530 Subject: [PATCH 18/18] check if geocoded address is not available in here maps --- providers/here.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/providers/here.js b/providers/here.js index b02db77..d56cf1d 100644 --- a/providers/here.js +++ b/providers/here.js @@ -49,7 +49,7 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { } var view = result.Response.View[0]; - if(!view) { + if(!view || !view.Result || !Array.isArray(view.Result) || !view.Result[0] || !view.Result[0].Location) { cbk(true, result); return; } @@ -75,6 +75,7 @@ exports.reverseGeocode = function ( providerOpts, lat, lng, opts, cbk ) { var location = view.Result[0].Location; if(location.Address) { + var a = location.Address; var additionalData = {};