-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
230 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import React, { useEffect } from "react"; | ||
import L from "leaflet"; | ||
import { MapContainer, TileLayer, FeatureGroup } from "react-leaflet"; | ||
import { EditControl } from "react-leaflet-draw"; | ||
import "leaflet/dist/leaflet.css"; | ||
import "leaflet-draw/dist/leaflet.draw.css"; | ||
import { useDispatch } from "react-redux"; | ||
import { Position, setPolygon } from "@/core/redux/slices/gridsSlice"; | ||
|
||
// Extend Leaflet types to include _getIconUrl | ||
interface IconDefault extends L.Icon { | ||
_getIconUrl?: string; | ||
} | ||
|
||
// Type for polygon data structure | ||
interface PolygonData { | ||
type: string; | ||
coordinates: Position[][] | Position[][][]; | ||
} | ||
|
||
// Type for draw control options | ||
interface DrawControlOptions { | ||
rectangle: boolean; | ||
circle: boolean; | ||
circlemarker: boolean; | ||
polyline: boolean; | ||
marker: boolean; | ||
} | ||
|
||
// Type for draw events | ||
interface DrawEvent { | ||
layerType: string; | ||
layer: L.Layer; | ||
} | ||
|
||
// Type for edit events | ||
interface EditEvent { | ||
layers: L.LayerGroup; | ||
} | ||
|
||
// Fix Leaflet's default icon issue | ||
delete (L.Icon.Default.prototype as IconDefault)._getIconUrl; | ||
L.Icon.Default.mergeOptions({ | ||
iconRetinaUrl: | ||
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-icon-2x.png", | ||
iconUrl: | ||
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-icon.png", | ||
shadowUrl: | ||
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-shadow.png", | ||
}); | ||
|
||
const PolygonMap: React.FC = () => { | ||
const dispatch = useDispatch(); | ||
const ZOOM_LEVEL = 10; | ||
|
||
const drawOptions: DrawControlOptions = { | ||
rectangle: false, | ||
circle: false, | ||
circlemarker: false, | ||
polyline: false, | ||
marker: false, | ||
}; | ||
|
||
const _created = (e: DrawEvent): void => { | ||
const { layerType, layer } = e; | ||
if (layerType === "polygon") { | ||
const polygon = layer as L.Polygon; | ||
const geoJson = polygon.toGeoJSON(); | ||
dispatch( | ||
setPolygon({ | ||
type: geoJson.geometry.type, | ||
coordinates: geoJson.geometry.coordinates, | ||
}) | ||
); | ||
} | ||
}; | ||
|
||
const _edited = (e: EditEvent): void => { | ||
const { layers } = e; | ||
layers.eachLayer((layer: L.Layer) => { | ||
if (layer instanceof L.Polygon) { | ||
const geoJson = layer.toGeoJSON(); | ||
dispatch( | ||
setPolygon({ | ||
type: geoJson.geometry.type, | ||
coordinates: geoJson.geometry.coordinates, | ||
}) | ||
); | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className="h-[400px] w-full bg-muted rounded-md overflow-hidden opacity-80"> | ||
<div className="w-full h-full relative"> | ||
<MapContainer | ||
center={{ lat: 0.347596, lng: 32.58252 }} | ||
zoom={ZOOM_LEVEL} | ||
className="h-full w-full" | ||
scrollWheelZoom={false} | ||
> | ||
<FeatureGroup> | ||
<EditControl | ||
position="topright" | ||
onCreated={_created} | ||
onEdited={_edited} | ||
draw={drawOptions} | ||
/> | ||
</FeatureGroup> | ||
<TileLayer | ||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' | ||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" | ||
/> | ||
</MapContainer> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default PolygonMap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.