· 4 years ago · Aug 11, 2021, 10:44 PM
1// Make Base URL for API request
2const baseURL = 'https://api.mapbox.com/geocoding/v5/mapbox.places/'
3
4// Query Select Search Button
5const searchButton = document.querySelector('#search');
6
7
8// API Req on Event Listener Search Button - Click / API Request API request to Mapbox using the data in the 'API key' and 'Address' text inputs
9searchButton.addEventListener('click', () => {
10
11 //Query select value of api key and address text inputs
12 const apiKey = document.querySelector('#api-key');
13 const address = document.querySelector('#address');
14
15 //Empty Variable
16 let data = '';
17
18 // API Request Function
19 async function mapBoxAPIRequest() {
20 const rawData = await fetch(baseURL + encodeURI(address.value) + '.json?access_token=' + apiKey.value)
21 const data = await rawData.json()
22 console.log(data.features[0].center[0]);
23 console.log(data.features[0].center[1]);
24 return data
25 }
26
27
28 // Call API Request Function
29 mapBoxAPIRequest();
30
31 console.log(data);
32 console.log(data.features[0].center[0]);
33 console.log(data.features[0].center[1]);
34
35})