Skip to content

Commit

Permalink
Merge pull request #158 from openmove/techpark
Browse files Browse the repository at this point in the history
upgrade geocoder here api
  • Loading branch information
RudiThoeni authored Oct 13, 2022
2 parents c556724 + e951a19 commit 6e7270c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 25 deletions.
2 changes: 1 addition & 1 deletion geocoder/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ endpoints:
##provided by npm heremap
appId: ${HERE_APPID}
appCode: ${HERE_APPCODE}
#apiKey: ${HERE_APIKEY}
apiKey: ${HERE_APIKEY}
size: 5
layer: address
boundary:
Expand Down
23 changes: 15 additions & 8 deletions geocoder/utils/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,28 @@ module.exports = (config, _) => {

lang = lang || config.default_lang;

const items = _.get(data,'body.Response.View[0].Result');
//const items = _.get(data,'body.Response.View[0].Result');
const items = _.get(data,'items');

return _.compact(_.map(items, (item)=> {
//console.log('HERE RETURN',JSON.stringify(items,null,4))

let lat = _.get(item,"Location.DisplayPosition.Latitude"),
return _.compact(_.map(items, item => {

/* let lat = _.get(item,"Location.DisplayPosition.Latitude"),
lon = _.get(item,"Location.DisplayPosition.Longitude"),
a = _.get(item,"Location.Address"),
text = _.compact([a.Street, a.HouseNumber, a.City]).join(', ');
text = _.compact([a.Street, a.HouseNumber, a.City]).join(', ');*/

let lat = _.get(item,"position.lat")
, lon = _.get(item,"position.lng")
, text = _.get(item,"address.label");

if (lat && lon) {
return createHit({
id: _.get(item,'Location.LocationId'),
text: text,
lat: lat,
lon: lon,
id: item.id,
text,
lat,
lon,
source: 'here',
layer: config.endpoints.here.layer
});
Expand Down
90 changes: 74 additions & 16 deletions geocoder/utils/services.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,112 @@

const heremap = require("heremap");
//const heremap = require("heremap");
//https://github.com/devbab/heremap#hm_geocode

const https = require('https');

const heremap = {
opts: {},
config: opts => {
heremap.opts = opts;
},
geocode: (text, params) => {

const {api_key} = heremap.opts;

var latitude = params.lat,
longitude = params.lon,
limit = params.maxresults,
country = params.country,
{maxLat, minLon, minLat, maxLon} = params.bbox,
//bbox = `${maxLat},${minLon},${minLat},${maxLon}`,
bbox = `${minLon},${minLat},${maxLon},${maxLat}`,
lang = params.language,
text = encodeURIComponent(text),
url = 'https://autosuggest.search.hereapi.com/v1/autosuggest?'
+`&apiKey=${api_key}`
+`&q=${text}`
//+`&lang=${lang}`
//+'&result_types=address,place'
+`&in=bbox:${bbox}`
//+`&in=countryCode:${country}`
+`&limit=${limit}`;

// https://stackoverflow.com/questions/68664953/here-autosuggest-get-complete-address-informations
console.log('HERE_REQUEST',url)

return new Promise((resolve, reject) => {
const req = https.request(url, res => {
if (res.statusCode===401) {
console.error(`Error to retrieve data, ${res.statusCode}`)
return
}
var str = "";
res.on('data', chunk => {
str += chunk;
});
res.on('end', () => {
try {
const data = JSON.parse(str);
resolve(data);
}
catch(err) {
console.error(`Error "${err}" to connect endpoint ${endpoint.hostname}${endpoint.path}`);
//reject(err)
}
});
})
.on('error', err => {
console.error(`Error "${err.code}" to connect endpoint ${endpoint.hostname}${endpoint.path}`);
//reject(err)
})
.end();
});
}
}

module.exports = (config, _) => {
return {
'here': async(text = '', lang) => {

if (!_.get(config,'endpoints.here.appId') ||
/* if (!_.get(config,'endpoints.here.appId') ||
!_.get(config,'endpoints.here.appCode') ||
config.endpoints.here.appId === '${HERE_APPID}' ||
config.endpoints.here.appCode === '${HERE_APPCODE}'
) {
console.warn("[geocoder] error in Endpoint: 'here' api appId/appCode not found");
console.warn("[geocoder] error in Endpoint: 'here' api appId/appCode/ not found");
return [];
}
}*/

heremap.config({
app_id: config.endpoints.here.appId,
app_code: config.endpoints.here.appCode
app_code: config.endpoints.here.appCode,
api_key: config.endpoints.here.apiKey
});

let bbox;
if(config.endpoints.here.boundary.rect) {
const {maxLat, minLon, minLat, maxLon} = config.endpoints.here.boundary.rect
bbox = `${maxLat},${minLon};${minLat},${maxLon}`;
/*boundary:
rect:
minLon: 10.470121
maxLon: 12.255011
minLat: 46.188280
maxLat: 47.088780*/
// TopLeft.Latitude,TopLeft.Longitude; BottomRight.Latitude,BottomRight.Longitude
}

//docs AUTOCOMPLETE https://developer.here.com/documentation/geocoder-autocomplete/dev_guide/topics/resource-suggest.html
//docs geocoder https://developer.here.com/documentation/geocoder/dev_guide/topics/api-reference.html
try {
//console.log('HERE',bbox,mapview)
const opts = {
const params = {
//search: text, //only AUTOCOMPLETE
maxresults: Number(config.endpoints.here.size),
country: 'ITA',
//resultType: 'street',//only AUTCOMPLETE
language: lang || config.server.default_lang,
bbox: bbox,
//bbox: bbox,
bbox: config.endpoints.here.boundary.rect,
mapview: bbox
};
//console.log('HERE REQUEST OPTS',opts)
return await heremap.geocode(text, opts);
//console.log('HERE REQUEST OPTS',params)
return await heremap.geocode(text, params);
}catch(err) {
console.log(err)
return []
}
}
Expand Down

0 comments on commit 6e7270c

Please sign in to comment.