-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmap.js
More file actions
310 lines (276 loc) · 11.4 KB
/
map.js
File metadata and controls
310 lines (276 loc) · 11.4 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { myUserId, myUserName, writeUserData, updateUserData, updateUserCounter } from './database.js';
const popupOptions = { closeOnClick: false, autoClose: false, autoPan: false, maxWidth: 150, maxHeight: 300 };
const defaultZoomLevel = 2;
// Function which finds and returns marker by id
// Use like this: map.getMarkerById(1234);
L.Map.include({
getMarkerById: function (id) {
let marker = null;
this.eachLayer(function (layer) {
if (layer instanceof L.Marker) {
if (layer.options.id === id) {
marker = layer;
}
}
});
return marker;
}
});
// Icon blue
let iconBlue = L.icon({
iconUrl: 'images/circle-blue.svg',
// shadowUrl: 'leaf-shadow.png',
iconSize: [25, 25], // size of the icon
// shadowSize: [50, 64], // size of the shadow
// iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
// shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-0, -15] // point from which the popup should open relative to the iconAnchor
});
// Icon red
let iconRed = L.icon({
iconUrl: 'images/circle-red.svg',
// shadowUrl: 'leaf-shadow.png',
iconSize: [25, 25], // size of the icon
// shadowSize: [50, 64], // size of the shadow
// iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
// shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-0, -15] // point from which the popup should open relative to the iconAnchor
});
// Map initialization
// export let map = L.map('map', { zoomControl: false, attributionControl: false }).setView([46.8182, 8.2275], defaultZoomLevel);
export let map = L.map('map', { zoomControl: false, attributionControl: false }).setView([0, 0], defaultZoomLevel);
// OSM layer
let osm = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
osm.addTo(map);
// Attribution control
let attributionControl = L.control.attribution({
// position: 'topright'
position: 'bottomright'
}).addTo(map);
map.attributionControl.setPrefix('<a href="https://www.aronsommer.com/">AS</a> | <a href="https://leafletjs.com/">Leaflet</a>');
// Zoom in button
L.DomEvent.on(L.DomUtil.get('buttonZoomIn'), 'click', function () {
map.setZoom(map.getZoom() + 1);
});
// Zoom out button
L.DomEvent.on(L.DomUtil.get('buttonZoomOut'), 'click', function () {
map.setZoom(map.getZoom() - 1);
});
// Add extras to map
import { addExtrasToMap } from './map-extras.js';
addExtrasToMap();
// Create feature group with my marker and circle
// The feature group will be added to map when updatePosition runs the first time
let myMarker, myCircle;
myMarker = L.marker([0, 0], { id: myUserId, icon: iconBlue });
myCircle = L.circle([0, 0], { radius: 0 });
let featureGroup = L.featureGroup([myMarker, myCircle]);
var myLat = 0;
var myLong = 0;
var intervalFunction;
// Check if my marker already exists
// if yes remove it from the map to avoid duplicate
export function checkIfMyMarkerAlreadyExists() {
if (map.getMarkerById(myUserId)) {
console.log("Your marker does already exist. Will remove it.");
map.removeLayer(map.getMarkerById(myUserId));
updateUserCounter(-1);
}
}
// This function gets called in database.js after signed in with Firebase anonymously
export function startUpdatingMyPosition() {
// Check if navigator.geolocation is available
if (!navigator.geolocation) {
console.log("Geolocation is not supported by this browser.");
} else {
// Execute the updatePosition function without delay the first time
navigator.geolocation.getCurrentPosition(updatePosition, showError);
// Execute the updatePosition function every 5 seconds
intervalFunction = setInterval(() => {
navigator.geolocation.getCurrentPosition(updatePosition, showError);
}, 5000);
}
}
// Show error if navigator.geolocation.getCurrentPosition fails
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
// Stop executing the updatePosition function
clearInterval(intervalFunction);
console.log("User denied the request for Geolocation.");
window.alert("You have disabled location tracking.\nPlease enable location tracking than refresh this page.");
break;
case error.POSITION_UNAVAILABLE:
console.log("Location information is unavailable.");
break;
case error.TIMEOUT:
console.log("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
console.log("An unknown error occurred.");
break;
}
}
// Update position of my marker
let updatePositionFirstTime = true;
function updatePosition(position) {
// console.log(position)
let lat;
let long;
let accuracy;
// Get exact position
lat = position.coords.latitude;
long = position.coords.longitude;
accuracy = position.coords.accuracy;
// Add offset if accuracyLevel = 0
if (accuracyLevel == 0) {
// let offset = ((Math.random() > 0.5 ? 0.0001 : -0.0009) + Math.random() * 0.0008);
let offset = ((Math.random() > 0.5 ? 0.01 : -0.09) + Math.random() * 0.08);
lat = lat + offset;
long = long + offset;
}
// Update position of myMarker and myCircle
myMarker.setLatLng([lat, long]);
myCircle.setLatLng([lat, long]);
myCircle.setRadius(accuracy);
myLat = lat;
myLong = long;
if (updatePositionFirstTime) {
// Write my user data and add my marker and circle to map with correct coordinates
writeUserData(myUserId, lat, long);
featureGroup.addTo(map);
// myMarker.bindPopup("This is you" + "<br>" + myUserId.substring(0, 7), popupOptions).openPopup();
myMarker.bindPopup("This is you" + "<br>" + myUserName, popupOptions).openPopup();
// Only go to your position the first time
// map.fitBounds(featureGroup.getBounds());
map.setView([myLat, myLong], defaultZoomLevel);
}
updatePositionFirstTime = false;
console.log("Your coordinates are\nLat: " + lat + " Long: " + long + " Accuracy: " + accuracy);
// Update user data in database.js
updateUserData(lat, long);
}
// Add new user marker
export function addNewUserMarker(userId, lat, long, txt = "", userName) {
// Marker with id to find later
let marker = new L.Marker([lat, long], { id: userId, icon: iconRed });
// marker.customID = userId;
// marker.on('click', onMarkerClick);
marker.addTo(map);
// marker.bindPopup(userId.substring(0, 7) + "<br>" + txt, popupOptions).openPopup();
marker.bindPopup(userName + "<br>" + txt, popupOptions).openPopup();
}
// Marker on click function
function onMarkerClick(e) {
alert(e.target.customID);
}
// Update user marker
export function updateMarker(userId, lat, long) {
console.log("User with userID " + userId + " has new coordinates" + "\nLat: " + lat + " Long: " + long);
let marker = map.getMarkerById(userId);
marker.setLatLng([lat, long]);
}
// Update marker text
export function updateMarkerText(userId, txt = "", userName) {
// Update my text
if (userId == myUserId) {
// Update text only if new text is different than the text in popup
let popupContent = myMarker._popup.getContent();
let popupContentWithoutUserId = popupContent.replace("This is you<br>" + myUserName.toString(), "");
popupContentWithoutUserId = popupContentWithoutUserId.replace("<br>", ""); // In case there is another <br>
// console.log(txt + " / " + popupContentWithoutUserId);
if (txt != popupContentWithoutUserId) {
if (myMarker.isPopupOpen()) {
// myMarker._popup.setContent("This is you" + "<br>" + myUserId.substring(0, 7) + "<br>" + txt, popupOptions).openPopup();
myMarker._popup.setContent("This is you" + "<br>" + myUserName + "<br>" + txt, popupOptions).openPopup();
}
if (!myMarker.isPopupOpen()) {
// myMarker.bindPopup("This is you" + "<br>" + myUserId.substring(0, 7) + "<br>" + txt, popupOptions).openPopup();
myMarker.bindPopup("This is you" + "<br>" + myUserName + "<br>" + txt, popupOptions).openPopup();
}
}
}
// Update text of user marker
if (userId != myUserId) {
let marker = map.getMarkerById(userId);
// Update text only if new text is different than the text in popup
let popupContent = marker._popup.getContent();
// let popupContentWithoutUserId = popupContent.replace(userId.substring(0, 7) + "<br>".toString(), "");
let popupContentWithoutUserId = popupContent.replace(userName + "<br>".toString(), "");
if (txt != popupContentWithoutUserId) {
console.log("User with userID " + userId + " has new text\n" + txt);
if (marker.isPopupOpen()) {
// marker._popup.setContent(userId.substring(0, 7) + "<br>" + txt, popupOptions).openPopup();
marker._popup.setContent(userName + "<br>" + txt, popupOptions).openPopup();
}
if (!marker.isPopupOpen()) {
// marker.bindPopup(userId.substring(0, 7) + "<br>" + txt, popupOptions).openPopup();
marker.bindPopup(userName + "<br>" + txt, popupOptions).openPopup();
}
}
}
}
// Remove user marker
export function removeMarker(userId) {
// map.getMarkerById(userId);
map.removeLayer(map.getMarkerById(userId));
}
// Find all markers than fit view to show all markers
export function zoomToFitAllMarkers() {
let markerBounds = L.latLngBounds([]);
let allMarkers = [];
allMarkers = getFeaturesInView();
if (allMarkers.length && allMarkers.length > 0) {
console.log("Found this markers: " + allMarkers);
allMarkers.forEach(marker => {
markerBounds.extend([marker.lat, marker.lng]);
});
// If I am the only user zoom to my marker
if (allMarkers.length == 1){
zoomToMyMarker(defaultZoomLevel)
}
if (allMarkers.length > 1){
map.fitBounds(markerBounds.pad(0.5));
}
}
if (allMarkers.length === 0) { console.log("Found no markers!"); }
}
// Search for all markers
function getFeaturesInView() {
var features = [];
map.eachLayer(function (layer) {
if (layer instanceof L.Marker) {
// if (map.getBounds().contains(layer.getLatLng())) {
// features.push(layer.feature);
features.push(layer.getLatLng());
// }
}
});
return features;
}
// Zoom to my marker
export function zoomToMyMarker(zoomLevel = 6) {
if (myLat != 0 && myLong != 0) {
// map.fitBounds(featureGroup.getBounds());
map.setView([myLat, myLong], zoomLevel);
}
if (myLat == 0 && myLong == 0) {
console.log("Your Lat and Long is 0");
}
}
// Change accuracy
let accuracyLevel = 0;
export function changeAccuracy() {
if (accuracyLevel == 0) {
accuracyLevel = 1;
document.getElementById("buttonChangeAccuracy").textContent = "Accuracy High";
console.log("accuracyLevel = 1");
}
else if (accuracyLevel == 1) {
accuracyLevel = 0;
document.getElementById("buttonChangeAccuracy").textContent = "Accuracy Low";
console.log("accuracyLevel = 0");
}
}