-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
78 lines (63 loc) · 2.71 KB
/
main.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
// const API_URL = "data.json"
const API_URL = "http://localhost:8080/"
const mapId = document.querySelector("#mapid");
let mymap = L.map(mapId).setView([55.951040, -3.186479], 13);
window.onload = () => {
L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibTJhOXg0NSIsImEiOiJjazgwZ3AzczcwZXJuM25tdzRpMGc0ajBkIn0.h4nHqQ9zYSLyP5bRiynMGg', {
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: 'streets-v11',
accessToken: 'pk.eyJ1IjoibTJhOXg0NSIsImEiOiJjamxqeWkxc2gwZm8xM3ByMDQzOXI5Z2x3In0.0AlI9PSaNnfUKy_bT9849A',
tileSize: 512,
zoomOffset: -1
}).addTo(mymap);
fetch(API_URL)
.then(res => res.json())
.then((data) => {
console.log(data);
for (let i = 0; i < data.data.dockGroups.length; i++) {
if (data.data.dockGroups[i].availabilityInfo.availableVehicleCategories[1].count >= 1) {
// this should give the info for each dock that has at least one e-bike
console.log(data.data.dockGroups[i]);
addmarker(data.data.dockGroups[i].coord.lat,data.data.dockGroups[i].coord.lng,
data.data.dockGroups[i].title, data.data.dockGroups[i].availabilityInfo.availableVehicleCategories[1].count);
}
}
})
.catch((err) => console.error(err))
}
function addmarker(lat, lon, name, ebikes) {
marker = L.marker([lat, lon],{"id":name, "ebikes":ebikes,"type":"marker"}).addTo(mymap).on("click", getLivecap)
.bindPopup("Loading...");
}
function getLivecap(e){
onMapClick(e);
let popup = e.target.getPopup();
let ebikes = this.options.ebikes;
let stationId = this.options.id;
popup.setContent(stationId + "<br>" + "e-bikes: " + ebikes + "<br>");
popup.update();
}
function onMapClick(e) {
console.log(e.target);
const myCustomColour = '#583470'
const markerHtmlStyles = `
background-color: ${myCustomColour};
width: 2rem;
height: 2rem;
display: block;
left: -1.5rem;
top: -1.5rem;
position: relative;
border-radius: 3rem 3rem 0;
transform: rotate(45deg);
border: 1px solid #FFFFFF`
const icon = L.divIcon({
className: "my-custom-pin",
iconAnchor: [0, 24],
labelAnchor: [-6, 0],
popupAnchor: [0, -36],
html: `<span style="${markerHtmlStyles}" />`
})
}
mymap.on('click', onMapClick);