From f57d9020d5031744e3e0e19ac8178cc8f7010f03 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 2 Aug 2022 07:11:16 -0700 Subject: Refactor map route plugin --- modern/src/map/MapCamera.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'modern/src/map/MapCamera.js') diff --git a/modern/src/map/MapCamera.js b/modern/src/map/MapCamera.js index 7ebf24fb..63f070f8 100644 --- a/modern/src/map/MapCamera.js +++ b/modern/src/map/MapCamera.js @@ -1,16 +1,28 @@ import { useEffect } from 'react'; - +import maplibregl from 'maplibre-gl'; import { map } from './core/MapView'; const MapCamera = ({ - latitude, longitude, + latitude, longitude, positions, }) => { useEffect(() => { - map.jumpTo({ - center: [longitude, latitude], - zoom: Math.max(map.getZoom(), 10), - }); - }, [latitude, longitude]); + if (positions) { + const 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[1])); + 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]); return null; }; -- cgit v1.2.3