parent
6729cc76a6
commit
9a8ead8238
5 changed files with 113 additions and 1 deletions
@ -0,0 +1,95 @@ |
||||
import React, { useState } from "react"; |
||||
|
||||
import { MapContainer, TileLayer, FeatureGroup } from "react-leaflet"; |
||||
import { EditControl } from "leaflet-draw"; |
||||
// import osm from "./osm-providers";
|
||||
import { useRef } from "react"; |
||||
|
||||
const LeafLet = () => { |
||||
const [center, setCenter] = useState({ lat: 24.4539, lng: 54.3773 }); |
||||
const [mapLayers, setMapLayers] = useState([]); |
||||
|
||||
const ZOOM_LEVEL = 12; |
||||
const mapRef = useRef(); |
||||
|
||||
const _onCreate = (e) => { |
||||
console.log(e); |
||||
|
||||
const { layerType, layer } = e; |
||||
if (layerType === "polygon") { |
||||
const { _leaflet_id } = layer; |
||||
|
||||
setMapLayers((layers) => [ |
||||
...layers, |
||||
{ id: _leaflet_id, latlngs: layer.getLatLngs()[0] }, |
||||
]); |
||||
} |
||||
}; |
||||
|
||||
const _onEdited = (e) => { |
||||
console.log(e); |
||||
const { |
||||
layers: { _layers }, |
||||
} = e; |
||||
|
||||
Object.values(_layers).map(({ _leaflet_id, editing }) => { |
||||
setMapLayers((layers) => |
||||
layers.map((l) => |
||||
l.id === _leaflet_id |
||||
? { ...l, latlngs: { ...editing.latlngs[0] } } |
||||
: l |
||||
) |
||||
); |
||||
}); |
||||
}; |
||||
|
||||
const _onDeleted = (e) => { |
||||
console.log(e); |
||||
const { |
||||
layers: { _layers }, |
||||
} = e; |
||||
|
||||
Object.values(_layers).map(({ _leaflet_id }) => { |
||||
setMapLayers((layers) => layers.filter((l) => l.id !== _leaflet_id)); |
||||
}); |
||||
}; |
||||
return ( |
||||
<> |
||||
{" "} |
||||
<div className="row"> |
||||
<div className="col text-center"> |
||||
<h2>React-leaflet - Create, edit and delete polygon on map</h2> |
||||
|
||||
<div className="col"> |
||||
<MapContainer center={center} zoom={ZOOM_LEVEL} ref={mapRef}> |
||||
<FeatureGroup> |
||||
<EditControl |
||||
position="topright" |
||||
onCreated={_onCreate} |
||||
onEdited={_onEdited} |
||||
onDeleted={_onDeleted} |
||||
draw={{ |
||||
rectangle: false, |
||||
polyline: false, |
||||
circle: false, |
||||
circlemarker: false, |
||||
marker: false, |
||||
}} |
||||
/> |
||||
</FeatureGroup> |
||||
|
||||
<TileLayer |
||||
attribution='&copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' |
||||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" |
||||
/> |
||||
</MapContainer> |
||||
|
||||
<pre className="text-left">{JSON.stringify(mapLayers, 0, 2)}</pre> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</> |
||||
); |
||||
}; |
||||
|
||||
export default LeafLet; |
After Width: | Height: | Size: 15 KiB |
Loading…
Reference in new issue