forked from liltimtim/mc-geocoding-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (63 loc) · 1.6 KB
/
index.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
62
63
64
65
66
const NodeGeocoder = require('node-geocoder');
class Geocode {
constructor(options = null) {
this.geocoder = NodeGeocoder(options);
}
/**
*
* @param {String} street
* @param {String} city
* @param {String} zipcode
* @param {String} state
*/
coordinatesOf(street, city, zipcode, state) {
return new Promise((resolve, reject) => {
this.geocoder.geocode(`${street} ${city} ${state} ${zipcode}`)
.then(result => {
if(result.length === 0) {
return reject('No results found.');
}
return resolve(result[0]);
})
.catch(err => {
return reject(err);
})
});
}
/**
* Attempts to resolve coordinates for the given address string.
* @param {String} address A partial or full address to attempt a resolution from
*/
coordinatesOfAddress(address) {
return new Promise((resolve, reject) => {
this.geocoder.geocode(address)
.then(result => {
if(result.length == 0) { return reject('No results found.'); }
return resolve(result[0]);
})
.catch(err => {
return reject(err);
});
});
}
/**
*
* @param {Number} latitude
* @param {Number} longitude
*/
addressOf(latitude, longitude) {
return new Promise((resolve, reject) => {
this.geocoder.reverse({lat: latitude, lon: longitude})
.then(result => {
if(result.length === 0) {
return reject('No results found.');
}
return resolve(result[0]);
})
.catch(err => {
return reject(err);
});
});
}
}
module.exports = Geocode;