I'm trying to implement a simple map editor, using React Leaflet on Typescript, in combination with react-leaflet-draw's EditControl.
The control should receive, in its properties, a set of blocks (a block is, basically, a polygon, with a serverId), and a set of handlers, called when a block is added/updated/deleted.
Displaying the initial blocks is fine, but when a block is edited or deleted, I don't manage to get the serverId of the modified/removed block.
How can this information be retrieved?
Here's my implementation:
import React, { FC } from 'react';
import { MapContainer, TileLayer, FeatureGroup, Polygon } from 'react-leaflet';
import { EditControl } from 'react-leaflet-draw';
import { LatLng } from 'leaflet';
import 'leaflet-draw/dist/leaflet.draw.css';
import 'leaflet-fullscreen/dist/Leaflet.fullscreen';
import 'leaflet-fullscreen/dist/leaflet.fullscreen.css';
import { If, Then } from 'react-if';
export interface Coordinate {
latitude: number;
longitude: number;
}
export interface Block {
serverId: number;
coordinates: Coordinate[];
}
interface MapEditorProps {
blocks: Block[];
onBlockCreated?: (coordinates: Coordinate[]) => void;
onBlockDeleted?: (blockId: number) => void;
onBlockUpdated?: (block: Block) => void;
}
const MapEditor: FC<MapEditorProps> = ({ blocks, onBlockCreated, onBlockDeleted, onBlockUpdated }: MapEditorProps) => {
const onCreated = (e) => {
const { layerType, layer } = e;
if (layerType === 'polygon') {
if (onBlockCreated !== undefined) {
const latlngs = layer.getLatLngs()[0];
onBlockCreated(latlngs.map((l: LatLng) => ({ latitude: l.lat, longitude: l.lng } as Coordinate)));
}
}
};
const onDeleted = (e) => {
if (onBlockDeleted !== undefined) {
// How to fetch the id of the deleted block?
// const blockId = ???
// onBlockDeleted (polygonId);
}
};
const onUpdated = (e) => {
if (onBlockUpdated !== undefined) {
// How to fetch the modified block?
// const updatedBlock = ???;
// onBlockUpdated (updatedBlock);
}
};
return (
<div>
<MapContainer
style={{ height: '100vh', width: '100wh' }}
center={{ lat: 44.72, lng: 8.4 }}
zoom={15}
fullscreenControl
>
<TileLayer
attribution=""
url="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
/>
<FeatureGroup>
<EditControl
position="topright"
onCreated={onCreated}
onEdited={onUpdated}
onDeleted={onDeleted}
draw={{
rectangle: false,
polyline: false,
circle: false,
circlemarker: false,
marker: true
}}
/>
<If condition={blocks !== null}>
<Then>
{blocks.map((block) => (
<Polygon key={block.serverId} positions={block.coordinates.map((c) => [c.latitude, c.longitude])} />
))}
</Then>
</If>
</FeatureGroup>
</MapContainer>
</div>
);
};
export default MapEditor;