-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhere-geocode.js
61 lines (54 loc) · 1.91 KB
/
here-geocode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module.exports = function(RED) {
function HEREGeoSearch( n ) {
RED.nodes.createNode(this,n );
var node = this;
var name = n.name;
var query = n.query;
var in_var = n.in;
var HEREConfigNode;
var apiKey;
const axios = require('axios');
// Retrieve the config node
HEREConfigNode = RED.nodes.getNode(n.apikey);
apiKey = HEREConfigNode.credentials.apikey;
node.on('input', function (msg) {
msg.hereparams = msg.hereparams || {};
//query, required parameter
if( typeof msg.hereparams.query == 'undefined' ) {
msg.hereparams.query = query; // take the default or the node setting
} else {
// passed in param, override default or node setting
msg.hereparams.query = msg.hereparams.query.toLowerCase();
}
// saving the api call in api_str variable
var api_str='https://geocode.search.hereapi.com/v1/geocode?q='+msg.hereparams.query+'&apiKey='+apiKey;
//in, optional parameter
if( typeof msg.hereparams.in == 'undefined' ) {
msg.hereparams.in = in_var; // take the default or the node setting
}
//Add the optional parameters to the api call if they are not empty
if(msg.hereparams.in != "") {
var area = msg.hereparams.in;
if( area.includes( "countryCode") ) {
api_str=api_str + '&in' + msg.hereparams.in;
} else {
api_str=api_str + '&in=countryCode:'+msg.hereparams.in;
}
}
(async () => {
try {
const response = await axios.get(api_str);
//console.log(response.data)
msg.payload = response.data;
node.send(msg);
} catch (error) {
console.log(error.response.data);
//console.log(error.response.status);
node.warn(error.response.data);
node.send(msg);
}
})();
});
}
RED.nodes.registerType("here-geocode",HEREGeoSearch);
}