-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocation.js
29 lines (26 loc) · 1.03 KB
/
Location.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
const GOOGLE_API_KEY = 'AIzaSyCueuEMBmaAK6XUl6pfsL0J5NTF6HwpjtY';
export async function getAddressFromCoords(coords) {
const response = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${coords.lat},${coords.lng}&key=${GOOGLE_API_KEY}`)
if (!response.ok) {
throw new Error('Failed to fetch address. Please try again!');
}
const data = await response.json();
if (data.error_message) {
throw new Error(data.error_message);
}
const address = data.results[0].formatted_address;
return address;
}
export async function getCoordsFromAddress(address) {
const urlAddress = encodeURI(address);
const response = await fetch(`https://maps.googleapis.com/maps/api/geocode/json?address=${urlAddress}&key=${GOOGLE_API_KEY}`);
if (!response.ok) {
throw new Error('Failed to fetch coordinates. Please try again!');
}
const data = await response.json();
if (data.error_message) {
throw new Error(data.error_message);
}
const coordinates = data.results[0].geometry.location;
return coordinates;
}