-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (49 loc) · 1.42 KB
/
index.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
'use strict';
const baseURL = 'https://bikewise.org:443/api/v2/locations/markers'
const options = {
method: 'GET',
mode: 'cors',
};
function getData(terms, location) {
const param = {
query: terms,
proximity: location
}
const qString = buildString(param)
const url = baseURL + '?' + qString
console.log(url)
fetch(url, options)
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error(response.statusText);
})
.then(responseJson => showInfo(responseJson))
.catch(err => {
$('#results').text(`Something went wrong: ${err.message}`);
});
}
function showInfo(responseJson) {
$('#results').empty()
let rf = responseJson.features
for (let i=0; i < responseJson.features.length; i++) {
$('#results').append(`<h2>${rf[i].properties.title}</h2>
<br><p>${rf[i].properties.description}</p><br>${rf[i].geometry.coordinates}<hr>`)
}
}
function buildString(param) {
const queryItems = Object.keys(param)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(param[key])}`)
return queryItems.join('&');
}
function readForm() {
$('form').submit(e => {
e.preventDefault()
const terms = $('#searchBar').val()
const location = $('#location').val()
console.log(terms, location)
getData(terms, location)
})
}
$(readForm)