From 6c0eece6465ec148ecc5517cfea5522c27a9801f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 26 Jun 2022 17:24:00 -0700 Subject: Implement geofence selection --- modern/src/map/MapGeofenceEdit.js | 19 ++++++++++++++++++- modern/src/other/GeofencesList.js | 6 +++--- modern/src/other/GeofencesPage.js | 8 +++++--- 3 files changed, 26 insertions(+), 7 deletions(-) (limited to 'modern') diff --git a/modern/src/map/MapGeofenceEdit.js b/modern/src/map/MapGeofenceEdit.js index 22f91dfa..045eddc2 100644 --- a/modern/src/map/MapGeofenceEdit.js +++ b/modern/src/map/MapGeofenceEdit.js @@ -1,5 +1,6 @@ import 'mapbox-gl/dist/mapbox-gl.css'; import '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css'; +import maplibregl from 'maplibre-gl'; import MapboxDraw from '@mapbox/mapbox-gl-draw'; import theme from '@mapbox/mapbox-gl-draw/src/lib/theme'; import { useEffect } from 'react'; @@ -35,7 +36,7 @@ const draw = new MapboxDraw({ }], }); -const MapGeofenceEdit = () => { +const MapGeofenceEdit = ({ selectedGeofenceId }) => { const dispatch = useDispatch(); const navigate = useNavigate(); @@ -136,6 +137,22 @@ const MapGeofenceEdit = () => { }); }, [geofences]); + useEffect(() => { + if (selectedGeofenceId) { + const feature = draw.get(selectedGeofenceId); + let { coordinates } = feature.geometry; + if (Array.isArray(coordinates[0][0])) { + [coordinates] = coordinates; + } + const bounds = coordinates.reduce( + (bounds, coordinate) => bounds.extend(coordinate), + new maplibregl.LngLatBounds(coordinates[0], coordinates[0]), + ); + const canvas = map.getCanvas(); + map.fitBounds(bounds, { padding: Math.min(canvas.width, canvas.height) * 0.1 }); + } + }, [selectedGeofenceId]); + return null; }; diff --git a/modern/src/other/GeofencesList.js b/modern/src/other/GeofencesList.js index 5463d73a..d26eff09 100644 --- a/modern/src/other/GeofencesList.js +++ b/modern/src/other/GeofencesList.js @@ -5,7 +5,7 @@ import { Divider, List, ListItemButton, ListItemText, } from '@mui/material'; -import { devicesActions, geofencesActions } from '../store'; +import { geofencesActions } from '../store'; import CollectionActions from '../settings/components/CollectionActions'; import { useCatchCallback } from '../reactHelper'; @@ -21,7 +21,7 @@ const useStyles = makeStyles(() => ({ }, })); -const GeofencesList = () => { +const GeofencesList = ({ onGeofenceSelected }) => { const classes = useStyles(); const dispatch = useDispatch(); @@ -40,7 +40,7 @@ const GeofencesList = () => { {Object.values(items).map((item, index, list) => ( - dispatch(devicesActions.select(item.id))}> + onGeofenceSelected(item.id)}> diff --git a/modern/src/other/GeofencesPage.js b/modern/src/other/GeofencesPage.js index 8af8a711..59dcdc4e 100644 --- a/modern/src/other/GeofencesPage.js +++ b/modern/src/other/GeofencesPage.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { useDispatch } from 'react-redux'; import { Divider, Typography, IconButton, useMediaQuery, Toolbar, @@ -62,6 +62,8 @@ const GeofencesPage = () => { const isPhone = useMediaQuery(theme.breakpoints.down('sm')); + const [selectedGeofenceId, setSelectedGeofenceId] = useState(); + const handleFile = (event) => { const files = Array.from(event.target.files); const [file] = files; @@ -117,11 +119,11 @@ const GeofencesPage = () => { - +
- + -- cgit v1.2.3