Skip to content

Commit 6d01ef3

Browse files
committed
fix: resolve bounds undefined error in price analytics
- Make bounds parameter optional in updateStatistics method - Add generateBoundsFromStations method to create bounds from station data - Store map reference in class for access across methods - Add fallback to UK bounds when no station data available - Fix createStatsOverlay method call to use stored map reference - Add proper bounds validation and generation
1 parent 337e2be commit 6d01ef3

1 file changed

Lines changed: 49 additions & 3 deletions

File tree

public/js/price-analytics.js

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class PriceAnalytics {
1010
this.overlayVisible = false;
1111
this.currentZoomLevel = 6;
1212
this.updateInterval = null;
13+
this.map = null;
1314

1415
// Lucide icon mappings (using kebab-case names)
1516
this.icons = {
@@ -664,15 +665,57 @@ class PriceAnalytics {
664665
document.body.appendChild(overlay);
665666
}
666667

668+
/**
669+
* Generate bounds from station data when bounds are not provided
670+
*/
671+
generateBoundsFromStations(stations) {
672+
if (!stations || !stations.features || stations.features.length === 0) {
673+
// Default to UK bounds if no stations
674+
return {
675+
west: -8.0,
676+
south: 49.5,
677+
east: 2.0,
678+
north: 59.0
679+
};
680+
}
681+
682+
let minLng = Infinity, minLat = Infinity;
683+
let maxLng = -Infinity, maxLat = -Infinity;
684+
685+
stations.features.forEach(station => {
686+
if (station.geometry && station.geometry.coordinates) {
687+
const [lng, lat] = station.geometry.coordinates;
688+
minLng = Math.min(minLng, lng);
689+
maxLng = Math.max(maxLng, lng);
690+
minLat = Math.min(minLat, lat);
691+
maxLat = Math.max(maxLat, lat);
692+
}
693+
});
694+
695+
// Add small padding to bounds
696+
const padding = 0.01;
697+
return {
698+
west: minLng - padding,
699+
south: minLat - padding,
700+
east: maxLng + padding,
701+
north: maxLat + padding
702+
};
703+
}
704+
667705
/**
668706
* Update statistics when map data changes
669707
*/
670-
updateStatistics(stations, bounds) {
708+
updateStatistics(stations, bounds = null) {
671709
if (!stations) return;
672710

711+
// Generate bounds if not provided
712+
if (!bounds) {
713+
bounds = this.generateBoundsFromStations(stations);
714+
}
715+
673716
const stats = this.calculateRegionalStats(stations, bounds);
674-
if (stats && this.overlayVisible) {
675-
this.createStatsOverlay(map, stats);
717+
if (stats) {
718+
this.createStatsOverlay(this.map, stats);
676719
}
677720
}
678721

@@ -1093,6 +1136,9 @@ class PriceAnalytics {
10931136
* Initialize price analytics
10941137
*/
10951138
init(map) {
1139+
// Store map reference
1140+
this.map = map;
1141+
10961142
// Add mobile-specific viewport meta tag if not present
10971143
if (this.isMobileDevice() && !document.querySelector('meta[name="viewport"]')) {
10981144
const viewport = document.createElement('meta');

0 commit comments

Comments
 (0)