60 lines
1.5 KiB
JavaScript
60 lines
1.5 KiB
JavaScript
const NodeGeocoder = require('node-geocoder');
|
|
|
|
function addressToString (address) {
|
|
var string = '';
|
|
string = address.address1 ? address.street1 + ',' : '';
|
|
string = string + (address.locality ? ' ' + address.locality + ',' : '');
|
|
string = string + (address.region ? ' ' + address.region : '');
|
|
string = string + (address.postal ? ' ' + address.postal : '');
|
|
string = string + (address.country ? ' ' + address.country : '');
|
|
return string;
|
|
}
|
|
|
|
var options = {
|
|
provider: 'google',
|
|
// Optional depending on the providers
|
|
httpAdapter: 'https', // Default
|
|
apiKey: 'AIzaSyCvpBGztvxtRUNigOW9f0GXVRWlukJZsps', // for Mapquest, OpenCage, Google Premier
|
|
formatter: null // 'gpx', 'string', ...
|
|
};
|
|
|
|
var geocoder = NodeGeocoder(options);
|
|
|
|
|
|
|
|
exports.addressToString = addressToString;
|
|
exports.geocoder = geocoder;
|
|
|
|
|
|
// Using callback
|
|
// geocoder.geocode('29 champs elysée paris', function(err, res) {
|
|
// console.log(res);
|
|
// });
|
|
//
|
|
// // Or using Promise
|
|
// geocoder.geocode('29 champs elysée paris')
|
|
// .then(function(res) {
|
|
// console.log(res);
|
|
// })
|
|
// .catch(function(err) {
|
|
// console.log(err);
|
|
// });
|
|
|
|
// output :
|
|
// [{
|
|
// latitude: 48.8698679,
|
|
// longitude: 2.3072976,
|
|
// country: 'France',
|
|
// countryCode: 'FR',
|
|
// city: 'Paris',
|
|
// zipcode: '75008',
|
|
// streetName: 'Champs-Élysées',
|
|
// streetNumber: '29',
|
|
// administrativeLevels: {
|
|
// level1long: 'Île-de-France',
|
|
// level1short: 'IDF',
|
|
// level2long: 'Paris',
|
|
// level2short: '75'
|
|
// },
|
|
// provider: 'google'
|
|
// }]
|