-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet-test.html
154 lines (141 loc) · 4.86 KB
/
leaflet-test.html
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link
rel="stylesheet"
href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""
/>
<style>
#mapid {
height: 500px;
}
</style>
<title>Open Maps</title>
</head>
<body>
<h1>Map area</h1>
<div id="mapid"></div>
<div id="output">Empty</div>
<button onclick="start('write')" type="button">Start</button>
<button onclick="start('view')" type="button">View</button>
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-firestore.js"></script>
<script
src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
crossorigin=""
></script>
<script>
var firebaseConfig = {
apiKey: "AIzaSyDsFjO3n2UQgoVpZ-JnsodhVVNB0oIS_jY",
authDomain: "bike-bus.firebaseapp.com",
databaseURL: "https://bike-bus.firebaseio.com",
projectId: "bike-bus",
storageBucket: "bike-bus.appspot.com",
messagingSenderId: "1090604910121",
appId: "1:1090604910121:web:c6d8b09244141a3a",
};
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
let mymap = L.map("mapid").setView([39.145681, -84.377785], 15);
let mode = "write";
let DataModel = {
name: "Patriots Park",
captain: "Evelyn",
tracking: [],
stops: [
{
lat: 39.146121,
long: -84.383743,
riders: [],
checkin: false,
},
{
lat: 39.146024,
long: -84.382079,
riders: [],
checkin: false,
},
],
};
const findLast = arr => arr[arr.length - 1];
const actions = {
tracking: payload => {
if (mode === "write") {
DataModel.tracking.push(payload);
console.log("writing to fb");
db.collection("routes")
.doc("zjktDwgFORQR1ovfafC5")
.set(DataModel);
console.log("done writing to fb");
} else {
console.log("viewing");
}
},
mapLocation: location => {
const lasttracking =
location.length > 1 ? findLast(location) : location[0];
console.log("lc ", lasttracking);
L.popup()
.setLatLng([lasttracking.latitude, lasttracking.longitude])
.setContent("Patriot Park (6)")
.openOn(mymap);
},
};
function update(tag, payload) {
actions[tag](payload);
console.log("datamodel updated ", DataModel);
}
function showPosition(position) {
console.log("showing position", position);
update("tracking", mapToWaypoint(position));
// update("mapLocation", "");
}
function start(optionMode) {
mode = optionMode;
console.log("Starting application");
navigator.geolocation.watchPosition(showPosition);
// Adding map layer to map area
L.tileLayer(
"https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?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",
accessToken:
"pk.eyJ1Ijoicm9ubnJvc3MiLCJhIjoiY2p4YzNxaHUzMGdhNTNvb2FsNW40aWNuZCJ9.T1hDMeUKJk3Dww5LfxeqaQ",
}
).addTo(mymap);
}
console.log('setting marker')
var marker = L.marker([39.146121, -84.383743]).addTo(mymap);
db.collection("routes")
.doc("zjktDwgFORQR1ovfafC5")
.onSnapshot(doc => {
// let changes = snapshot.docChanges();
let updates = doc.data();
update("mapLocation", updates.tracking);
// changes.forEach(change => {
// if (change.type === "added") {
// //render change.doc.data()
// } else if (change.type === "removed") {
// //remove
// }
// });
});
const mapToWaypoint = position => {
return {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
time: position.timestamp,
};
};
</script>
</body>
</html>