Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Area selection by ctrl key with mouse for SAM #897

Merged
merged 8 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/libs/components/src/map/AreaSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Select area by ctrl + mouse
*/

// several type errors because leaflet-area-select does not have type definition
// so ignore it for now
// @ts-nocheck
import { useEffect } from 'react';
import { useMap } from 'react-leaflet';
import L from 'leaflet';
// load modified leaflet-area-select directly
import './AreaSelectHack.js';
import { Bounds as BoundsProp } from './Types';

interface AreaSelectProps {
setBoxCoord: (value: React.SetStateAction<BoundsProp | undefined>) => void;
}

export default function AreaSelect({ setBoxCoord }: AreaSelectProps) {
const map = useMap();

useEffect(() => {
if (!map.selectArea) return;

// map focus
map.getContainer().focus();

map.selectArea.enable();

// use ctrl key
map.selectArea.setControlKey(true);

// get coordinates of selected area
map.on('areaselected', (e) => {
const coordinates = e.bounds.toBBoxString().split(',').map(Number);
// coordinates format is SW lng & lon and NE lng & lon
// so converting to SW lat & lng and NE lat & lng and set boxCoord
setBoxCoord({
southWest: { lat: coordinates[1], lng: coordinates[0] },
northEast: { lat: coordinates[3], lng: coordinates[2] },
});
});

// restrict selection area
const bounds = map.getBounds().pad(-0.25);
// check restricted area on start and move
map.selectArea.setValidate((layerPoint) => {
return bounds.contains(this._map.layerPointToLatLng(layerPoint));
});

// switch it off
map.selectArea.setValidate();
}, []);

return null;
}
Loading
Loading