-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_view.html
More file actions
368 lines (344 loc) Β· 17.5 KB
/
map_view.html
File metadata and controls
368 lines (344 loc) Β· 17.5 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trolley Map - Reported Abandoned Trolleys</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.4.1/dist/MarkerCluster.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.4.1/dist/MarkerCluster.Default.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script>
<script src="https://unpkg.com/leaflet.markercluster@1.4.1/dist/leaflet.markercluster.js"></script>
<style>
/* Styles adapted from your existing files */
:root {
--bg-color: #1a1a1a;
--text-color: #e0e0e0;
--card-bg: #2c2c2c; /* For container background */
--border-color: #444;
--button-bg: #007bff; /* Default primary button */
--button-secondary-bg: #555;
--button-secondary-hover-bg: #777;
--button-text: #ffffff;
--rating-inactive-bg: #444;
--rating-inactive-text: #ccc;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
height: 100%; /* Ensure full height */
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
line-height: 1.6;
background-color: var(--bg-color);
color: var(--text-color);
display: flex; /* Use flexbox for centering */
flex-direction: column; /* Stack children vertically */
align-items: center; /* Center horizontally */
padding: 5px;
}
.container {
width: 100%;
min-width: 100%;
max-width: 95%; /* Adjust as needed */
margin: 5px auto;
padding: 10px; /* Slightly more padding */
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
background-color: var(--card-bg);
border: 1px solid var(--border-color);
display: flex; /* Use flexbox for internal layout */
flex-direction: column;
flex-grow: 1; /* Allow container to take available space */
}
h1 {
color: var(--text-color);
margin-top: 0; /* Remove default margin */
margin-bottom: 15px;
font-size: 1.8em;
text-align: center;
border-bottom: 1px solid var(--border-color);
padding-bottom: 10px;
}
#map {
width: 100%;
/* height: 450px; */ /* Let flexbox control height */
flex-grow: 1; /* Allow map to take available vertical space */
border-radius: 5px;
border: 1px solid var(--border-color);
margin-bottom: 15px; /* Space below map */
}
/* Styles for Brand Links Section */
.brand-links-container {
margin-top: 5px; /* Reduced margin */
padding-top: 5px;
border-top: 1px dashed var(--border-color);
text-align: center;
}
.brand-links-container h2 {
font-size: 1.1em;
color: var(--text-color);
margin-bottom: 10px; /* Increased space */
font-weight: normal;
}
.brand-links {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 10px;
padding: 0;
list-style: none;
}
.brand-link-button {
display: inline-block;
padding: 5px 10px;
font-size: 0.9em;
text-align: center;
border-radius: 5px;
border: 1px solid transparent;
text-decoration: none;
cursor: pointer;
transition: background-color 0.2s ease, border-color 0.2s ease;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
min-width: 100px;
color: var(--button-text);
}
/* Brand Specific Colors for Links */
.brand-link-button.brand-coles { background-color: #ed1c22; color: white; border-color: #ed1c22; }
.brand-link-button.brand-coles:hover { background-color: #c8191e; border-color: #c8191e;}
.brand-link-button.brand-woolworths { background-color: #54b848; color: white; border-color: #54b848; }
.brand-link-button.brand-woolworths:hover { background-color: #47a03d; border-color: #47a03d;}
.brand-link-button.brand-aldi { background-color: #00005F; color: white; border-color: #00005F; }
.brand-link-button.brand-aldi:hover { background-color: #00004a; border-color: #00004a;}
.brand-link-button.brand-iga { background-color: #D8C69D; color: #333333; border-color: #D8C69D; }
.brand-link-button.brand-iga:hover { background-color: #c2b28d; border-color: #c2b28d;}
.brand-link-button.brand-kmart { background-color: #E92A28; color: white; border-color: #E92A28; }
.brand-link-button.brand-kmart:hover { background-color: #c72321; border-color: #c72321;}
.brand-link-button.brand-bigw { background-color: #0046C8; color: white; border-color: #0046C8; }
.brand-link-button.brand-bigw:hover { background-color: #0037a0; border-color: #0037a0;}
.brand-link-button.brand-bunnings { background-color: #0d5257; color: white; border-color: #0d5257; }
.brand-link-button.brand-bunnings:hover { background-color: #0b494e; border-color: #0b494e;}
.brand-link-button.brand-spudshed { background-color: #a6cf4d; color: #8b380a; border-color: #0d5257; }
.brand-link-button.brand-spudshed:hover { background-color: #84a53d; border-color: #0b494e;}
.brand-link-button.brand-other { background-color: var(--rating-inactive-bg); color: var(--rating-inactive-text); border-color: var(--border-color);}
.brand-link-button.brand-other:hover { background-color: var(--button-secondary-bg); border-color: var(--button-secondary-bg);}
/* End Brand Specific Colors */
/* Back Link Styles */
.back-link-container {
text-align: center;
margin-top: 20px; /* Reduced margin */
padding-top: 15px;
border-top: 1px dashed var(--border-color);
flex-shrink: 0; /* Prevent shrinking */
}
.back-link {
display: inline-block;
padding: 10px 15px;
background-color: var(--button-bg);
color: var(--button-text);
border-radius: 5px;
text-decoration: none;
transition: background-color 0.2s ease;
margin: 5px; /* Use margin for spacing */
}
.back-link:hover {
background-color: var(--button-secondary-hover-bg);
text-decoration: none;
color: var(--button-text);
}
/* Responsive Adjustments */
@media (max-width: 768px) {
body { padding: 1px; } /* Adjust padding for smaller screens */
.container { padding: 5px; max-width: 100%; }
h1 { font-size: 1.5em; }
#map { min-height: 350px; } /* Ensure minimum height */
.brand-link-button { font-size: 0.85em; padding: 8px 10px; min-width: 80px;}
.back-link {
padding: 8px 12px;
font-size: 0.9em;
}
}
/* Style for Leaflet marker cluster icons */
.marker-cluster-small {
background-color: rgba(181, 226, 140, 0.6);
}
.marker-cluster-small div {
background-color: rgba(110, 204, 57, 0.6);
}
.marker-cluster-medium {
background-color: rgba(241, 211, 87, 0.6);
}
.marker-cluster-medium div {
background-color: rgba(240, 194, 12, 0.6);
}
.marker-cluster-large {
background-color: rgba(253, 156, 115, 0.6);
}
.marker-cluster-large div {
background-color: rgba(241, 128, 23, 0.6);
}
.marker-cluster div {
width: 30px;
height: 30px;
margin-left: 5px;
margin-top: 5px;
text-align: center;
border-radius: 15px;
font: 12px "Helvetica Neue", Arial, Helvetica, sans-serif;
color: #fff; /* White text for better contrast */
line-height: 30px; /* Vertically center text */
text-shadow: 1px 1px 1px #000; /* Text shadow for readability */
}
.marker-cluster {
border-radius: 50%; /* Make cluster icons circular */
box-shadow: 0 0 5px rgba(0,0,0,0.5); /* Add shadow */
}
</style>
</head>
<body>
<div class="container">
<h1>Trolley Map</h1>
<div id="map"></div> <div class="brand-links-container">
<h2>Brand Maps</h2>
<div class="brand-links">
<a href="map_coles.html" class="brand-link-button brand-coles">CO</a>
<a href="map_woolworths.html" class="brand-link-button brand-woolworths">WO</a>
<a href="map_aldi.html" class="brand-link-button brand-aldi">AL</a>
<a href="map_iga.html" class="brand-link-button brand-iga">IG</a>
<a href="map_kmart.html" class="brand-link-button brand-kmart">KM</a>
<a href="map_bigw.html" class="brand-link-button brand-bigw">BI</a>
<a href="map_bunnings.html" class="brand-link-button brand-bunnings">BU</a>
<a href="map_spudshed.html" class="brand-link-button brand-spudshed">SP</a>
<a href="map_other.html" class="brand-link-button brand-other">Other</a>
</div>
</div>
<div class="back-link-container">
<a href="index.html" class="back-link">Report</a>
<a href="view_logs.php" class="back-link">List</a>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
console.log("DOM Ready, initializing map...");
const mapElement = document.getElementById('map');
if (!mapElement) { console.error("Map container element not found!"); return; }
// Define initial map center and zoom level
const initialCenter = [-31.9523, 115.8613]; // Perth approx center
const initialZoom = 9; // Zoom level for initial view
const map = L.map('map').setView(initialCenter, initialZoom);
console.log("Map initialized");
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
console.log("Tile layer added");
// ** NEW: Initialize MarkerClusterGroup **
const markerClusterGroup = L.markerClusterGroup();
map.addLayer(markerClusterGroup); // Add the group to the map
console.log("MarkerClusterGroup added to map");
// Invalidate map size after a short delay to ensure proper rendering
setTimeout(() => { map.invalidateSize(); console.log("Map size invalidated."); }, 200);
async function loadMarkers() {
console.log("Loading markers...");
try {
// Fetch log data
const response = await fetch('trolley_logs.json?_=' + new Date().getTime());
if (!response.ok) throw new Error(`HTTP error! Status: ${response.status}`);
const logs = await response.json();
if (!Array.isArray(logs)) throw new Error("Invalid data format received.");
console.log(`Fetched ${logs.length} logs.`);
// ** Clear previous markers from the cluster group **
markerClusterGroup.clearLayers();
let markersFound = 0;
// Process each log entry
logs.forEach((log, index) => {
const lat = parseFloat(log.latitude);
const lon = parseFloat(log.longitude);
// Validate coordinates
if (!isNaN(lat) && !isNaN(lon) && Math.abs(lat) <= 90 && Math.abs(lon) <= 180) {
const marker = L.marker([lat, lon]);
// Create popup content
let popupContent = `<b>Brand(s):</b> ${Array.isArray(log.brands) ? log.brands.map(escapeHtml).join(', ') : 'N/A'}<br>`;
popupContent += `<b>Reported:</b> ${formatDisplayDateTime(log.serverTimestampUTC || log.timestamp)}<br>`;
if (log.suburb && log.state) {
popupContent += `<b>Location:</b> ${escapeHtml(log.suburb)}, ${escapeHtml(log.state)}<br>`;
}
let conditionsText = getConditionsText(log.conditions);
if (conditionsText) {
popupContent += `<b>Condition(s):</b> ${escapeHtml(conditionsText)}<br>`;
}
if (log.comments) {
popupContent += `<b>Comments:</b> ${escapeHtml(log.comments)}`;
}
marker.bindPopup(popupContent);
// ** Add marker to the cluster group instead of a layerGroup **
markerClusterGroup.addLayer(marker);
markersFound++;
} else {
console.warn(`Skipping log entry ${index} with invalid coordinates:`, log);
}
});
console.log(`Added ${markersFound} markers to cluster group.`);
// Handle cases with no markers
if (markersFound === 0) {
if (mapElement) mapElement.innerHTML = '<p style="text-align:center; padding-top: 20px;">No trolley locations found in logs.</p>';
// Reset to default view if no markers
map.setView(initialCenter, initialZoom);
} else {
// ** Fit bounds using the markerClusterGroup **
const bounds = markerClusterGroup.getBounds();
if (bounds.isValid()) {
map.fitBounds(bounds, { padding: [50, 50], maxZoom: 16 }); // Add padding and maxZoom
console.log("Map bounds fit to marker cluster group.");
} else {
console.warn("Marker cluster bounds are not valid, cannot fit bounds. Resetting view.");
map.setView(initialCenter, initialZoom); // Fallback view
}
}
map.invalidateSize(); // Ensure map redraws correctly
} catch (error) {
console.error("Error loading map markers:", error);
if (mapElement) mapElement.innerHTML = `<p style="text-align:center; padding-top: 20px; color: red;">Error loading map data: ${error.message}</p>`;
}
}
// --- Helper Functions ---
function formatDisplayDateTime(isoTimestamp) {
// ... (keep the existing function) ...
if (!isoTimestamp) return 'N/A';
try {
const date = new Date(isoTimestamp);
if (isNaN(date.getTime())) {
const alternativeDate = new Date(isoTimestamp.replace('T', ' ').replace(/-/g, '/'));
return !isNaN(alternativeDate.getTime())
? alternativeDate.toLocaleString('en-AU', { timeZone: 'Australia/Perth', dateStyle: 'short', timeStyle: 'short', hour12: true })
: isoTimestamp;
}
return date.toLocaleString('en-AU', { timeZone: 'Australia/Perth', dateStyle: 'short', timeStyle: 'short', hour12: true });
} catch (e) { console.error("Date formatting error:", e); return isoTimestamp; }
}
function getConditionsText(conditions) {
// ... (keep the existing function) ...
if (typeof conditions !== 'object' || conditions === null) return '';
let allConditions = [];
for (const brand in conditions) {
if (Array.isArray(conditions[brand])) allConditions.push(...conditions[brand]);
}
// Return unique conditions, joined by comma
return [...new Set(allConditions)].join(', ');
}
function escapeHtml(text) {
// ... (keep the existing function) ...
if (!text) return '';
const map = {'&': '&','<': '<','>': '>','"': '"',"'": '''};
return text.replace(/[&<>"']/g, m => map[m]);
}
// Load markers on initial page load
loadMarkers();
});
</script>
</body>
</html>