diff options
Diffstat (limited to 'src/map/MapCamera.js')
-rw-r--r-- | src/map/MapCamera.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/map/MapCamera.js b/src/map/MapCamera.js new file mode 100644 index 00000000..afb52f89 --- /dev/null +++ b/src/map/MapCamera.js @@ -0,0 +1,32 @@ +import { useEffect } from 'react'; +import maplibregl from 'maplibre-gl'; +import { map } from './core/MapView'; + +const MapCamera = ({ + latitude, longitude, positions, coordinates, +}) => { + useEffect(() => { + if (coordinates || positions) { + if (!coordinates) { + coordinates = positions.map((item) => [item.longitude, item.latitude]); + } + if (coordinates.length) { + const bounds = coordinates.reduce((bounds, item) => bounds.extend(item), new maplibregl.LngLatBounds(coordinates[0], coordinates[0])); + const canvas = map.getCanvas(); + map.fitBounds(bounds, { + padding: Math.min(canvas.width, canvas.height) * 0.1, + duration: 0, + }); + } + } else { + map.jumpTo({ + center: [longitude, latitude], + zoom: Math.max(map.getZoom(), 10), + }); + } + }, [latitude, longitude, positions, coordinates]); + + return null; +}; + +export default MapCamera; |