From f5ce1cfbefefb4616233beb60e907d3f7d0a25d8 Mon Sep 17 00:00:00 2001 From: stefanocudini Date: Tue, 11 Oct 2022 15:58:10 +0200 Subject: [PATCH 1/4] change request --- geocoder/config.yml | 2 +- geocoder/utils/services.js | 81 +++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/geocoder/config.yml b/geocoder/config.yml index ecf00e92..0591d36c 100644 --- a/geocoder/config.yml +++ b/geocoder/config.yml @@ -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: diff --git a/geocoder/utils/services.js b/geocoder/utils/services.js index 858a190d..46f602c5 100644 --- a/geocoder/utils/services.js +++ b/geocoder/utils/services.js @@ -1,7 +1,65 @@ -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}`; + lang = params.language, + url = 'https://autosuggest.search.hereapi.com/v1/autosuggest?'+ + `apiKey=${api_key}&lang=${lang}&q=${text}&limit=${limit}`+ + '&result_types=address,place'+ + `&in=bbox:${bbox}&in=countryCode:${country}`; + //&at=${latitude},${longitude} + + + // 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); + console.log('RETURN',data.items[0]) + resolve({}); + } + 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) => { @@ -11,43 +69,38 @@ module.exports = (config, _) => { 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) { return [] } From 2a82ee2b1f1294a281720cc410db7659eb7f76de Mon Sep 17 00:00:00 2001 From: stefanocudini Date: Tue, 11 Oct 2022 17:10:36 +0200 Subject: [PATCH 2/4] fix bboxes --- geocoder/utils/services.js | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/geocoder/utils/services.js b/geocoder/utils/services.js index 46f602c5..ed779b78 100644 --- a/geocoder/utils/services.js +++ b/geocoder/utils/services.js @@ -14,21 +14,26 @@ const heremap = { const {api_key} = heremap.opts; var latitude = params.lat, - longitude = params.lon, - limit = params.maxresults, - country = params.country, + longitude = params.lon, + limit = params.maxresults, + country = params.country, {maxLat, minLon, minLat, maxLon} = params.bbox, - bbox = `${maxLat},${minLon},${minLat},${maxLon}`; - lang = params.language, - url = 'https://autosuggest.search.hereapi.com/v1/autosuggest?'+ - `apiKey=${api_key}&lang=${lang}&q=${text}&limit=${limit}`+ - '&result_types=address,place'+ - `&in=bbox:${bbox}&in=countryCode:${country}`; - //&at=${latitude},${longitude} - + //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) + console.log('HERE_REQUEST',url) + return new Promise((resolve, reject) => { const req = https.request(url, res => { if (res.statusCode===401) { @@ -42,7 +47,7 @@ console.log('HERE_REQUEST',url) res.on('end', () => { try { const data = JSON.parse(str); - console.log('RETURN',data.items[0]) + console.log('RETURN',JSON.stringify(data,null,4)) resolve({}); } catch(err) { @@ -64,14 +69,14 @@ 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"); return []; - } + }*/ heremap.config({ app_id: config.endpoints.here.appId, @@ -102,6 +107,7 @@ module.exports = (config, _) => { //console.log('HERE REQUEST OPTS',params) return await heremap.geocode(text, params); }catch(err) { + console.log(err) return [] } } From aaf9abeed64a400c4ee526540f606385d8bf3e6c Mon Sep 17 00:00:00 2001 From: stefanocudini Date: Wed, 12 Oct 2022 11:08:59 +0200 Subject: [PATCH 3/4] fix here reformat --- geocoder/utils/formatters.js | 24 ++++++++++++++++-------- geocoder/utils/services.js | 5 ++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/geocoder/utils/formatters.js b/geocoder/utils/formatters.js index b10e272e..f398e6a8 100644 --- a/geocoder/utils/formatters.js +++ b/geocoder/utils/formatters.js @@ -64,21 +64,29 @@ 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"); +console.log(item.id,lat,lon,text) 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 }); diff --git a/geocoder/utils/services.js b/geocoder/utils/services.js index ed779b78..36e379bb 100644 --- a/geocoder/utils/services.js +++ b/geocoder/utils/services.js @@ -26,7 +26,7 @@ const heremap = { +`&apiKey=${api_key}` +`&q=${text}` //+`&lang=${lang}` - +'&result_types=address,place' + //+'&result_types=address,place' +`&in=bbox:${bbox}` //+`&in=countryCode:${country}` +`&limit=${limit}`; @@ -47,8 +47,7 @@ const heremap = { res.on('end', () => { try { const data = JSON.parse(str); - console.log('RETURN',JSON.stringify(data,null,4)) - resolve({}); + resolve(data); } catch(err) { console.error(`Error "${err}" to connect endpoint ${endpoint.hostname}${endpoint.path}`); From e951a19c356e0591501d588ff65e6bb87b1c9508 Mon Sep 17 00:00:00 2001 From: stefanocudini Date: Wed, 12 Oct 2022 11:09:25 +0200 Subject: [PATCH 4/4] fix here reformat --- geocoder/utils/formatters.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/geocoder/utils/formatters.js b/geocoder/utils/formatters.js index f398e6a8..ced35b8b 100644 --- a/geocoder/utils/formatters.js +++ b/geocoder/utils/formatters.js @@ -67,7 +67,7 @@ module.exports = (config, _) => { //const items = _.get(data,'body.Response.View[0].Result'); const items = _.get(data,'items'); - console.log('HERE RETURN',JSON.stringify(items,null,4)) + //console.log('HERE RETURN',JSON.stringify(items,null,4)) return _.compact(_.map(items, item => { @@ -79,9 +79,8 @@ module.exports = (config, _) => { let lat = _.get(item,"position.lat") , lon = _.get(item,"position.lng") , text = _.get(item,"address.label"); -console.log(item.id,lat,lon,text) - if (lat && lon) { + if (lat && lon) { return createHit({ id: item.id, text,