diff --git a/heatmap-tool/frontend/src/components/MapBoundsManager.tsx b/heatmap-tool/frontend/src/components/MapBoundsManager.tsx new file mode 100644 index 00000000..73a2dce2 --- /dev/null +++ b/heatmap-tool/frontend/src/components/MapBoundsManager.tsx @@ -0,0 +1,26 @@ +// src/components/MapBoundsManager.tsx +import React, { useEffect } from 'react'; +import { useMap } from 'react-leaflet'; +import L from 'leaflet'; +import { HeatmapPoint } from '../App'; + +interface MapBoundsManagerProps { + data: HeatmapPoint[]; +} + +const MapBoundsManager: React.FC = ({ data }) => { + const map = useMap(); + + useEffect(() => { + if (data && data.length > 0) { + const bounds = L.latLngBounds(data.map(p => [p.lat, p.lon])); + if (bounds.isValid()) { + map.fitBounds(bounds, { padding: [50, 50] }); + } + } + }, [data, map]); + + return null; // This component does not render anything +}; + +export default MapBoundsManager; diff --git a/heatmap-tool/frontend/src/components/MapDisplay.tsx b/heatmap-tool/frontend/src/components/MapDisplay.tsx index 7a648c4e..39d71432 100644 --- a/heatmap-tool/frontend/src/components/MapDisplay.tsx +++ b/heatmap-tool/frontend/src/components/MapDisplay.tsx @@ -7,6 +7,7 @@ import 'leaflet.heat'; import type { HeatmapPoint, MapMode } from '../App'; import MarkerClusterGroup from 'react-leaflet-cluster'; import Legend from './Legend'; +import MapBoundsManager from './MapBoundsManager'; interface MapDisplayProps { heatmapData: HeatmapPoint[]; @@ -84,6 +85,7 @@ const MapDisplay: React.FC = ({ heatmapData, radiusMultiplier, url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" attribution='© OpenStreetMap contributors' /> + {viewMode === 'points' ? renderPoints() : renderHeatmap()} {viewMode === 'points' && }