|
24 | 24 | <script src="./lib/leaflet/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script> |
25 | 25 | <script> |
26 | 26 | fetch('/checkins') |
27 | | - .then(response => { return response.json() }) |
| 27 | + .then(response => response.json()) |
28 | 28 | .then(coordinates => { |
29 | | - const startingEntry = Math.floor(Math.random() * ((coordinates.length - 1) - 0 + 1) + 0); |
| 29 | + const startingEntry = Math.floor(Math.random() * coordinates.length); |
30 | 30 | let startLat = coordinates[startingEntry].lat; |
31 | 31 | let startLong = coordinates[startingEntry].long; |
32 | 32 |
|
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); |
41 | 39 |
|
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); |
47 | 42 |
|
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); |
50 | 47 |
|
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 | + } |
58 | 67 | }) |
59 | 68 | .catch(error => { |
60 | 69 | console.error('Error fetching data:', error); |
|
0 commit comments