-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
77 lines (59 loc) · 2.06 KB
/
script.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
const submitButton = document.querySelector('#submit');
const baseURL = `https://infinite-chamber-21931.herokuapp.com/data`;
const responseSection = document.querySelector('#response-section');
const locationButton = document.querySelector('#location');
// this key is restricted
const KEY = 'AIzaSyAu44VqU4eP5QFVUKg1DSSGc9yzXFZOHlI';
const url = 'https://maps.googleapis.com/maps/api/place/photo';
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
document.querySelector('#lat').value = latitude;
document.querySelector('#lng').value = longitude;
}
function error() {
console.log('OOppppssssss!!!!');
}
function currentLocation(){
if (!navigator.geolocation) {
console.log('Some error in location');
} else {
navigator.geolocation.getCurrentPosition(success, error);
}
}
locationButton.addEventListener('click', currentLocation);
async function data(event) {
event.preventDefault();
submitButton.textContent = 'Loading...';
lat = document.querySelector('#lat').value;
lng = document.querySelector('#lng').value;
let type = document.querySelector('#type').value;
const radius = document.querySelector('#radius').value;
type = type.toLowerCase()
const formData = { lat, lng, type, radius };
const options = {
method: "POST",
headers: {
"Content-type": "application/json"
},
body: JSON.stringify(formData)
}
const response = await fetch(baseURL, options);
const respData = await response.json()
submitButton.textContent = 'Submit';
let output = ``;
respData.forEach(async element => {
output += `<div class="wrapper card">
<div class="img">
<img src="${url}?maxwidth=400&photoreference=${element.photos[0].photo_reference}&key=${KEY}" alt="">
</div>
<ul>
<li><b>${element.name}</b></li>
<li>Rating- ${element.rating}</li>
<li>Address- ${element.vicinity}</li>
</ul>
</div>`;
});
responseSection.innerHTML = output;
}
submitButton.addEventListener('click', data);