-
Notifications
You must be signed in to change notification settings - Fork 56
/
Geocoder.js
183 lines (158 loc) · 4.95 KB
/
Geocoder.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Module to use google's geocoding & reverse geocoding.
*/
let Geocoder;
export default Geocoder = {
apiKey : null,
options : {},
/**
* Initialize the module.
* @param {String} apiKey The api key of your application in google.
* @param {Object} [options] extra options for your geocoding request.
* @see https://developers.google.com/maps/documentation/geocoding/intro#geocoding
*/
init(apiKey, options = {}) {
this.apiKey = apiKey;
this.options = options;
},
/**
* @returns {boolean} True if the module has been initiated. False otherwise.
*/
get isInit() {
return !!this.apiKey;
},
/**
* Do <a href="https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding">(reverse) geocoding</a>, converting geographic coordinates into a human-readable address & vice-versa.
* Accepted parameters:
* <ul>
* <li>from(Number latitude, Number longitude)</li>
* <li>from(Array [latitude, longitude])</li>
* <li>from(Object {latitude, longitude})</li>
* <li>from(Object {lat, lng})</li>
* <li>from(String address)</li>
* </ul>
* @returns {Promise.<Object>} Object containing informations about the place at the coordinates.
* @see https://developers.google.com/maps/documentation/geocoding/intro#GeocodingResponses
*/
async from(...params) {
// check api key
if (!Geocoder.isInit)
throw {
code : Geocoder.Errors.NOT_INITIATED,
message : "Geocoder isn't initialized. Call Geocoder.init function (only once), passing it your app's api key as parameter.",
};
// --- convert parameters ---
let queryParams;
// (latitude, longitude)
if (!isNaN(params[0]) && !isNaN(params[1]))
queryParams = {latlng : `${params[0]},${params[1]}`};
// [latitude, longitude]
else if (params[0] instanceof Array)
queryParams = {latlng : `${params[0][0]},${params[0][1]}`};
// {latitude, longitude} or {lat, lng}
else if (params[0] instanceof Object)
queryParams = {latlng : `${params[0].lat || params[0].latitude},${params[0].lng || params[0].longitude}`};
// address, {bounds: {northeast: {lat, lng}, southwest: {lan, lng}}}
else if (typeof params[0] === 'string' && params[1] instanceof Object)
queryParams = {address : params[0], bounds : params[1]};
// address
else if (typeof params[0] === 'string')
queryParams = {address : params[0]};
// --- start geocoding ---
// check query params
if (!queryParams)
// no query params, means parameters where invalid
throw {
code : Geocoder.Errors.INVALID_PARAMETERS,
message : "Invalid parameters : \n" + JSON.stringify(params, null, 2),
};
queryParams = { key: this.apiKey, ...this.options, ...queryParams }
// build url
const url = `https://maps.google.com/maps/api/geocode/json?${toQueryParams(queryParams)}`;
let response, data;
// fetch
try {
response = await fetch(url);
} catch(error) {
throw {
code : Geocoder.Errors.FETCHING,
message : "Error while fetching. Check your network.",
origin : error,
};
}
// parse
try {
data = await response.json();
} catch(error) {
throw {
code : Geocoder.Errors.PARSING,
message : "Error while parsing response's body into JSON. The response is in the error's 'origin' field. Try to parse it yourself.",
origin : response,
};
}
// check response's data
if (data.status !== 'OK')
throw {
code : Geocoder.Errors.SERVER,
message : "Error from the server while geocoding. The received datas are in the error's 'origin' field. Check it for more informations.",
origin : data,
};
return data;
},
/**
* All possible errors.
*/
Errors : {
/**
* Module hasn't been initiated. Call {@link Geocoder.init}.
*/
NOT_INITIATED : 0,
/**
* Parameters are invalid.
*/
INVALID_PARAMETERS : 1,
/**
* Error wile fetching to server.
* The error.origin property contains the original fetch error.
*/
FETCHING : 2,
/**
* Error while parsing server response.
* The error.origin property contains the response.
*/
PARSING : 3,
/**
* Error from the server.
* The error.origin property contains the response's body.
*/
SERVER : 4,
},
}
/**
* Encodes a bounds object into a URL encoded-string.
*/
function encodeBounds(bounds) {
const southwest = bounds.southwest;
const northeast = bounds.northeast;
return `${encodeURIComponent(southwest.lat)},${encodeURIComponent(southwest.lng)}|${encodeURIComponent(northeast.lat)},${encodeURIComponent(northeast.lng)}`;
}
/**
* Encodes a component so it can be used safely inside a URL.
*/
function encodeComponent(key, value) {
if (key === 'bounds') {
return encodeBounds(value);
}
return encodeURIComponent(value);
}
/**
* Convert an object into query parameters.
* @param {Object} object Object to convert.
* @returns {string} Encoded query parameters.
*/
function toQueryParams(object) {
return Object.keys(object)
.filter(key => !!object[key])
.map(key => key + "=" + encodeComponent(key, object[key]))
.join("&")
}