-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (71 loc) · 1.78 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*!
* latlng-address
* Copyright(c) 2017 Shunsuke <qfoori@gmail.com>
* ISC Licensed
*/
'use strict';
const GoogleMapsAPI = require('googlemaps');
const LANGUAGE = 'en';
module.exports = class LatLngAddress {
constructor(config) {
this.config = {};
if (config) {
this.config.key = config.key;
this.config.stagger_time = config.stagger_time || 1000;
this.config.encode_polylines = config.encode_polylines || false;
this.config.secure = config.secure || true;
}
if (config && config.key && config.key.length > 0) {
this.gm = new GoogleMapsAPI(config);
}
}
_latlng(lat, lng) {
if (!lat || !lng) {
return;
}
let latlng = '';
latlng += String(lat);
latlng += ',';
latlng += String(lng);
return latlng;
}
getAddress(lat, lng, options) {
return new Promise((resolve, reject) => {
const gm = this.gm;
if (!gm) {
const err = new Error('invalid config');
reject(err);
return;
}
const latlng = this._latlng(lat, lng);
if (!latlng || latlng.length === 0) {
const err = new Error('invalid latitude/longitude');
reject(err);
return;
}
let lang = LANGUAGE;
if (options) {
if (options.language) {
lang = options.language;
}
}
const reverseGeocodeParams = {
result_type : 'postal_code',
language : lang,
location_type: 'APPROXIMATE',
latlng : latlng
};
gm.reverseGeocode(reverseGeocodeParams, (err, result) => {
if (err) {
reject(err);
return;
}
if (result && result.status === 'OK') {
resolve(result.results);
return;
}
resolve(null);
});
});
}
}