-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
89 lines (64 loc) · 2.25 KB
/
test.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
80
81
82
83
84
85
86
87
88
89
'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}`)
var lat = `${rf[i].geometry.coordinates[1]}`
var lon = `${rf[i].geometry.coordinates[0]}`
//$('#results').append(`<div id="mapid"></div><hr>`)
addMaps(lat, lon)
}
}
function addMaps(lat, lon) {
var mymap = L.map('mapid').setView([lat, lon], 13);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken: 'pk.eyJ1IjoiZmluazYxMiIsImEiOiJja2gyNW02cngwOGk2MnptejR3bGY1eHdyIn0.1vtv_OlheSQGcC6akM79Tg'
}).addTo(mymap);
}
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)