97 lines
2.1 KiB
JavaScript
97 lines
2.1 KiB
JavaScript
const NodeGeocoder = require('node-geocoder');
|
|
const logger = require('log4js').getLogger('GeoCode');
|
|
|
|
const { reduceOpencage } = require('./reducers/opencage');
|
|
logger.level = 'debug';
|
|
|
|
const options = {
|
|
'provider': 'opencage',
|
|
|
|
// Optional depending on the providers
|
|
'httpAdapter': 'https', // Default
|
|
'apiKey': '893ab539eca84b5ca7a54cb03ef23443', // for Mapquest, OpenCage, Google Premier
|
|
'formatter': null // 'gpx', 'string', ...
|
|
};
|
|
|
|
const geocoder = NodeGeocoder(options);
|
|
|
|
function doGetGeocode(ll) {
|
|
return new Promise((resolve, reject) => {
|
|
const [lat, lon ] = ll.split(',');
|
|
|
|
const latlong = { lat, lon };
|
|
|
|
logger.debug(latlong);
|
|
|
|
geocoder.reverse(latlong)
|
|
.then(function(res) {
|
|
if (res.hasOwnProperty('raw')) {
|
|
const result = reduceOpencage(res.raw);
|
|
|
|
return resolve(result[0]);
|
|
}
|
|
else
|
|
return resolve(res[0]);
|
|
})
|
|
.catch(function(err) {
|
|
logger.error(err);
|
|
|
|
return reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
module.exports = { doGetGeocode };
|
|
|
|
/*
|
|
|
|
opencage
|
|
|
|
{
|
|
"latitude": 51.508751,
|
|
"longitude": -0.067457,
|
|
"country": "United Kingdom",
|
|
"city": "London",
|
|
"state": "England",
|
|
"zipcode": "SE15",
|
|
"streetName": "Vaughan Way",
|
|
"countryCode": "gb",
|
|
"suburb": "St.George in the East",
|
|
"extra": {
|
|
"flag": "🇬🇧",
|
|
"confidence": 9,
|
|
"confidenceKM": 0.5,
|
|
"map": "https://www.openstreetmap.org/?mlat=51.50875&mlon=-0.06746#map=17/51.50875/-0.06746"
|
|
}
|
|
}
|
|
|
|
google
|
|
|
|
[
|
|
{
|
|
"administrativeLevels": {
|
|
"level1long": "England",
|
|
"level1short": "England",
|
|
"level2long": "Northamptonshire",
|
|
"level2short": "Northamptonshire"
|
|
},
|
|
"city": "Northampton",
|
|
"country": "United Kingdom",
|
|
"countryCode": "GB",
|
|
"extra": {
|
|
"confidence": 0.7,
|
|
"establishment": "Daventy depot",
|
|
"googlePlaceId": "ChIJI8H0WFUVd0gRIIFzNwDQAuM",
|
|
"neighborhood": "Kilsby",
|
|
"premise": null,
|
|
"subpremise": null
|
|
},
|
|
"formattedAddress": "Daventy depot, Kilsby, Northampton NN6 7GY, UK",
|
|
"latitude": 52.3546726,
|
|
"longitude": -1.1741823,
|
|
"provider": "google",
|
|
"zipcode": "NN6 7GY"
|
|
}
|
|
]
|
|
*/
|