Skip to content

Commit f40c824

Browse files
committed
fixed issue with device's coords not being used in time for map starting position, even if available.
1 parent ac628bf commit f40c824

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

src/wwwroot/index.html

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,46 @@
2424
<script src="./lib/leaflet/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
2525
<script>
2626
fetch('/checkins')
27-
.then(response => { return response.json() })
27+
.then(response => response.json())
2828
.then(coordinates => {
29-
const startingEntry = Math.floor(Math.random() * ((coordinates.length - 1) - 0 + 1) + 0);
29+
const startingEntry = Math.floor(Math.random() * coordinates.length);
3030
let startLat = coordinates[startingEntry].lat;
3131
let startLong = coordinates[startingEntry].long;
3232

33-
// If possible, centre the map in on the device location instead of random.
34-
if (navigator.geolocation)
35-
{
36-
navigator.geolocation.getCurrentPosition(pos => {
37-
startLat = pos.coords.latitude;
38-
startLong = pos.coords.longitude;
39-
}, err => { });
40-
}
33+
function init(lat, long) {
34+
const map = L
35+
.map('map', {
36+
preferCanvas: true
37+
})
38+
.setView([lat, long], 3);
4139

42-
const map = L
43-
.map('map', {
44-
preferCanvas: true
45-
})
46-
.setView([ startLat, startLong ], 3);
40+
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png')
41+
.addTo(map);
4742

48-
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png')
49-
.addTo(map);
43+
coordinates.forEach(point => {
44+
const marker = L
45+
.marker([point.lat, point.long])
46+
.addTo(map);
5047

51-
coordinates.forEach(point => {
52-
const marker = L
53-
.marker([point.lat, point.long])
54-
.addTo(map);
55-
56-
marker.bindPopup(`<strong>${point.dateTimeUtc}</strong><br>${point.note}`);
57-
});
48+
marker.bindPopup(`<strong>${point.dateTimeUtc}</strong><br>${point.note}`);
49+
});
50+
}
51+
52+
if (navigator.geolocation) {
53+
navigator.geolocation.getCurrentPosition(
54+
pos => {
55+
startLat = pos.coords.latitude;
56+
startLong = pos.coords.longitude;
57+
init(startLat, startLong);
58+
},
59+
err => {
60+
console.warn('Geolocation failed or denied, using fallback coordinates.');
61+
init(startLat, startLong);
62+
}
63+
);
64+
} else {
65+
init(startLat, startLong);
66+
}
5867
})
5968
.catch(error => {
6069
console.error('Error fetching data:', error);

0 commit comments

Comments
 (0)