-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGeoman.tsx
94 lines (88 loc) · 2.57 KB
/
Geoman.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import * as React from 'react'
import { FeatureGroup } from 'react-leaflet'
import type { FeatureCollection } from 'geojson'
import * as L from 'leaflet'
import { GeomanControls } from '../src/index'
interface Props {
geojson: FeatureCollection
setGeojson: (geojson: FeatureCollection) => void
}
export default function Geoman({ geojson, setGeojson }: Props) {
const ref = React.useRef<L.FeatureGroup>(null)
React.useEffect(() => {
if (ref.current?.getLayers().length === 0 && geojson) {
L.geoJSON(geojson).eachLayer((layer) => {
if (
layer instanceof L.Polyline ||
layer instanceof L.Polygon ||
layer instanceof L.Marker
) {
if (layer?.feature?.properties.radius && ref.current) {
new L.Circle(layer.feature.geometry.coordinates.slice().reverse(), {
radius: layer.feature?.properties.radius,
}).addTo(ref.current)
} else {
ref.current?.addLayer(layer)
}
}
})
}
}, [geojson])
const handleChange = () => {
const newGeo: FeatureCollection = {
type: 'FeatureCollection',
features: [],
}
const layers = ref.current?.getLayers()
if (layers) {
layers.forEach((layer) => {
if (layer instanceof L.Circle || layer instanceof L.CircleMarker) {
const { lat, lng } = layer.getLatLng()
newGeo.features.push({
type: 'Feature',
properties: {
radius: layer.getRadius(),
},
geometry: {
type: 'Point',
coordinates: [lng, lat],
},
})
} else if (
layer instanceof L.Marker ||
layer instanceof L.Polygon ||
layer instanceof L.Rectangle ||
layer instanceof L.Polyline
) {
newGeo.features.push(layer.toGeoJSON())
}
})
}
setGeojson(newGeo)
}
return (
<FeatureGroup ref={ref}>
<GeomanControls
options={{
position: 'topleft',
drawText: false,
}}
globalOptions={{
continueDrawing: true,
editable: false,
}}
// onMount={() => L.PM.setOptIn(true)}
// onUnmount={() => L.PM.setOptIn(false)}
eventDebugFn={console.log}
onCreate={handleChange}
onChange={handleChange}
onUpdate={handleChange}
onEdit={handleChange}
onMapRemove={handleChange}
onMapCut={handleChange}
onDragEnd={handleChange}
onMarkerDragEnd={handleChange}
/>
</FeatureGroup>
)
}