This repository was archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathgoogleApi.js
79 lines (70 loc) · 2.39 KB
/
googleApi.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
const googleUrl = 'https://maps.google.com/maps/api/geocode/json';
function format(raw) {
const address = {
position: {},
formattedAddress: raw.formatted_address || '',
feature: null,
streetNumber: null,
streetName: null,
postalCode: null,
locality: null,
country: null,
countryCode: null,
adminArea: null,
subAdminArea: null,
subLocality: null,
};
if (raw.geometry && raw.geometry.location) {
address.position = {
lat: raw.geometry.location.lat,
lng: raw.geometry.location.lng,
}
}
raw.address_components.forEach(component => {
if (component.types.indexOf('route') !== -1) {
address.streetName = component.long_name;
}
else if (component.types.indexOf('street_number') !== -1) {
address.streetNumber = component.long_name;
}
else if (component.types.indexOf('country') !== -1) {
address.country = component.long_name;
address.countryCode = component.short_name;
}
else if (component.types.indexOf('locality') !== -1) {
address.locality = component.long_name;
}
else if (component.types.indexOf('postal_code') !== -1) {
address.postalCode = component.long_name;
}
else if (component.types.indexOf('administrative_area_level_1') !== -1) {
address.adminArea = component.long_name;
}
else if (component.types.indexOf('administrative_area_level_2') !== -1) {
address.subAdminArea = component.long_name;
}
else if (component.types.indexOf('sublocality') !== -1 || component.types.indexOf('sublocality_level_1') !== -1) {
address.subLocality = component.long_name;
}
else if (component.types.indexOf('point_of_interest') !== -1 || component.types.indexOf('colloquial_area') !== -1) {
address.feature = component.long_name;
}
});
return address;
}
export default {
geocodePosition(apiKey, position) {
return this.geocodeRequest(`${googleUrl}?key=${apiKey}&latlng=${position.lat},${position.lng}`);
},
geocodeAddress(apiKey, address) {
return this.geocodeRequest(`${googleUrl}?key=${apiKey}&address=${encodeURI(address)}`);
},
async geocodeRequest(url) {
const res = await fetch(url);
const json = await res.json();
if (!json.results || json.status !== 'OK') {
return Promise.reject(new Error(`geocoding error ${json.status}, ${json.error_message}`));
}
return json.results.map(format);
}
}