Skip to content

Added an additional here geocoder which provides suggestions to given… #210

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

Open
wants to merge 8 commits into
base: main
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ var geocoder = NodeGeocoder({

* `google` : GoogleGeocoder. Supports address geocoding and reverse geocoding. Use `options.clientId`and `options.apiKey`(privateKey) for business licence. You can also use `options.language` and `options.region` to specify language and region, respectively. Note that 'https' is required when using an apiKey
* `here` : HereGeocoder. Supports address geocoding and reverse geocoding. You must specify `options.appId` and `options.appCode` with your license keys. You can also use `options.language`, `options.politicalView` ([read about political views here](https://developer.here.com/rest-apis/documentation/geocoder/topics/political-views.html)), `options.country`, and `options.state`.
* `here with suggestions`: HereGeocoderWithSuggestions. The here geocoder with suggestions to a given search term.
* `freegeoip` : FreegeoipGeocoder. Supports IP geocoding
* `datasciencetoolkit` : DataScienceToolkitGeocoder. Supports IPv4 geocoding and address geocoding. Use `options.host` to specify a local instance
* `openstreetmap` : OpenStreetMapGeocoder. Supports address geocoding and reverse geocoding. You can use `options.language` and `options.email` to specify a language and a contact email address.
Expand Down
3 changes: 3 additions & 0 deletions lib/geocoder/heregeocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ HereGeocoder.prototype._geocode = function (value, callback) {
params.postalcode = value.zipcode;
}
params.searchtext = value.address;
}
else if (value.locationId) {
params.locationId = value.locationId;
} else {
params.searchtext = value;
}
Expand Down
80 changes: 80 additions & 0 deletions lib/geocoder/heregeocoderwithsuggestions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Created by caliskan on 15.02.17.
*/

var util = require('util'),
HereGeocoder = require('./heregeocoder');

/**
* Constructor
* @param <object> httpAdapter Http Adapter
* @param <object> options Options (appId, appCode, language, politicalView, country, state)
*/
var HereGeocoderWithSuggestions = function HereGeocoderWithSuggestions(httpAdapter, options) {
this.options = ['appId', 'appCode', 'language', 'politicalView', 'country', 'state'];

HereGeocoderWithSuggestions.super_.call(this, httpAdapter, options);
};

util.inherits(HereGeocoderWithSuggestions, HereGeocoder);

// Here geocoder suggestion API endpoint
HereGeocoderWithSuggestions.prototype._suggestionsEndpoint = 'https://autocomplete.geocoder.api.here.com/6.2/suggest.json';

/**
* Geocode
* @param <string> value Value to geocode
* @param <function> callback Callback method
*/
HereGeocoderWithSuggestions.prototype._geocode = function (value, callback) {
var params = this._prepareQueryString();

if (value.query) {
params.query = value.query;
if (value.country) {
params.country = value.country;
}
} else {
params.query = value;
}

var self = this;
var geocodeResults = [];
geocodeResults.raw = [];
var suggestionsLength = 0;

// first get some suggestions
this.httpAdapter.get(this._suggestionsEndpoint, params, getSuggestions);

function getSuggestions (err, result) {
if (err) {
return callback(err, result);
} else {
suggestionsLength = result.suggestions.length;

if (0 === suggestionsLength) {
return callback(false, geocodeResults);
}

// geocode each suggestion with its locationId, so that lat/lng is available
result.suggestions.forEach(function (item) {
HereGeocoder.prototype._geocode.call(self, {locationId: item.locationId}, formatSuggestions);
});
}
}

function formatSuggestions (err, result) {
if (err) {
return callback(err, result);
} else {
geocodeResults.push(result[0]);
geocodeResults.raw.push(result.raw);

if (geocodeResults.length === suggestionsLength) {
return callback(false, geocodeResults);
}
}
}
}

module.exports = HereGeocoderWithSuggestions;
13 changes: 8 additions & 5 deletions lib/geocoder/locationiqgeocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ LocationIQGeocoder.prototype._geocode = function(value, callback) {
// when there’s no err thrown here the resulting array object always
// seemes to be defined but empty so no need to check for
// responseData.error for now

var results = responseData.map(this._formatResult).filter(function(result) {
return result.longitude && result.latitude;
});
results.raw = responseData;
// add check if the array is not empty, as it returns an empty array from time to time
var results = [];
if (responseData.length && responseData.length > 0) {
results = responseData.map(this._formatResult).filter(function(result) {
return result.longitude && result.latitude;
});
results.raw = responseData;
}

callback(false, results);
}.bind(this));
Expand Down
4 changes: 4 additions & 0 deletions lib/geocoderfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var RequestAdapter = require('./httpadapter/requestadapter.js');

var GoogleGeocoder = require('./geocoder/googlegeocoder.js');
var HereGeocoder = require('./geocoder/heregeocoder.js');
var HereGeocoderWithSuggestions = require('./geocoder/heregeocoderwithsuggestions.js');
var AGOLGeocoder = require('./geocoder/agolgeocoder.js');
var FreegeoipGeocoder = require('./geocoder/freegeoipgeocoder.js');
var DataScienceToolkitGeocoder = require('./geocoder/datasciencetoolkitgeocoder.js');
Expand Down Expand Up @@ -58,6 +59,9 @@ var GeocoderFactory = {
if (geocoderName === 'here') {
return new HereGeocoder(adapter, {appId: extra.appId, appCode: extra.appCode, language: extra.language, politicalView: extra.politicalView, country: extra.country, state: extra.state});
}
if (geocoderName === 'herewithsuggestions') {
return new HereGeocoderWithSuggestions(adapter, {appId: extra.appId, appCode: extra.appCode, language: extra.language, politicalView: extra.politicalView, country: extra.country, state: extra.state});
}
if (geocoderName === 'agol') {
return new AGOLGeocoder(adapter, {client_id: extra.client_id, client_secret: extra.client_secret});
}
Expand Down
Loading