An open API service indexing awesome lists of open source software.

https://github.com/alejandrorm-dev/leaflet-editable-hook

The purpose of this hook is to provide functionality for editing features on the map, such as drawing polylines, polygons, markers, rectangles, circles, and more.
https://github.com/alejandrorm-dev/leaflet-editable-hook

Last synced: 8 months ago
JSON representation

The purpose of this hook is to provide functionality for editing features on the map, such as drawing polylines, polygons, markers, rectangles, circles, and more.

Awesome Lists containing this project

README

          

# Leaflet Editable Hook

[![npm version](https://img.shields.io/npm/v/leaflet-editable-hook.svg?style=flat-square)](https://www.npmjs.com/package/leaflet-editable-hook)
[![npm downloads](https://img.shields.io/npm/dm/leaflet-editable-hook.svg?style=flat-square)](https://www.npmjs.com/package/leaflet-editable-hook)
[![GitHub stars](https://img.shields.io/github/stars/AlejandroRM-DEV/leaflet-editable-hook?style=flat-square)](https://github.com/AlejandroRM-DEV/leaflet-editable-hook/stargazers)
[![MIT License](https://img.shields.io/npm/l/leaflet-editable-hook.svg?style=flat-square)](LICENSE)
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-)

The purpose of this hook is to provide functionality for editing features on the map, such as drawing polylines, polygons, markers, rectangles, circles, and more.

## Demo

https://leaflet-editable-hook.vercel.app/

## Prerequisites

- leaflet: "^1.9.4"
- leaflet-editable: "^1.2.0"
- react: "^19.0.0"
- react-dom: "^19.0.0"
- react-leaflet: "^5.0.0"

## Installation

Install with npm

```bash
npm i leaflet-editable-hook
```

## Documentation

[See full documentation about Leaflet.Editable](https://leaflet.github.io/Leaflet.Editable/doc/api.html)

| Hook returned methods | Returns | Description |
| ------------------------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| drawing() | boolean | Return true if any drawing action is ongoing. |
| stopDrawing() | void | When you need to stop any ongoing drawing, without needing to know which editor is active. |
| commitDrawing() | void | When you need to commit any ongoing drawing, without needing to know which editor is active. |
| startPolyline( latlng, options) | L.Polyline | Start drawing a Polyline. If latlng is given, a first point will be added. In any case, continuing on user click. If options is given, it will be passed to the Polyline class constructor. |
| startPolygon( latlng, options) | L.Polygon | Start drawing a Polygon. If latlng is given, a first point will be added. In any case, continuing on user click. If options is given, it will be passed to the Polygon class constructor. |
| startMarker( latlng, options) | L.Marker | Start adding a Marker. If latlng is given, the Marker will be shown first at this point. In any case, it will follow the user mouse, and will have a final latlng on next click (or touch). If options is given, it will be passed to the Marker class constructor. |
| startRectangle( latlng, options) | L.Rectangle | Start drawing a Rectangle. If latlng is given, the Rectangle anchor will be added. In any case, continuing on user drag. If options is given, it will be passed to the Rectangle class constructor. |
| startCircle( latlng, options) | L.Circle | Start drawing a Circle. If latlng is given, the Circle anchor will be added. In any case, continuing on user drag. If options is given, it will be passed to the Circle class constructor. |
| enableEdit(feature: L.Layer) | void | Enable editing, by creating an editor if not existing, and then calling enable on it. |
| disableEdit(feature: L.Layer) | void | Disable editing, also remove the editor property reference. |

| Events |
| ----------------------- |
| onCreated |
| onEnable |
| onDisable |
| onEditing |
| onDragStart |
| onDrag |
| onDragEnd |
| onDrawingStart |
| onDrawingEnd |
| onDrawingCancel |
| onDrawingCommit |
| onDrawingMousedown |
| onDrawingMouseup |
| onDrawingClick |
| onDrawingMove |
| onDrawingClicked |
| onVertexNew |
| onVertexClick |
| onVertexClicked |
| onVertexRawclick |
| onVertexDeleted |
| onVertexCtrlclick |
| onVertexShiftclick |
| onVertexMetakeyclick |
| onVertexAltclick |
| onVertexContextmenu |
| onVertexMousedown |
| onVertexDrag |
| onVertexDragstart |
| onVertexDragend |
| onMiddlemarkerMousedown |
| onShapeNew |
| onShapeDelete |
| onShapeDeleted |

## Usage/Examples

```javascript
import { useMap, Marker, Tooltip } from "react-leaflet";
import { useState, useEffect } from "react";
import * as turf from "@turf/turf";
import { useLeafletEditable } from "leaflet-editable-hook";

function Ruler() {
const map = useMap();
const [ruler, setRuler] = useState(null);

const onDrawingClicked = (e) => {
const position = e.latlng;
const latlngs = e.layer._latlngs;
let length = 0;
if (latlngs.length > 1) {
const line = turf.lineString([
...latlngs.map((item) => [item.lng, item.lat]),
]);
length = turf.length(line, { units: "kilometers" });
}
setRuler(

);
};

const onVertexDrag = (e) => {
const latlngs = e.layer._latlngs;
let length = 0;
if (latlngs.length > 1) {
const line = turf.lineString([
...latlngs.map((item) => [item.lng, item.lat]),
]);
length = turf.length(line, { units: "kilometers" });
}
setRuler(

);
};

const { startPolyline } = useLeafletEditable({
events: {
onDrawingClicked,
onVertexDrag,
},
});

useEffect(() => {
const polyline = startPolyline();
return () => {
map.removeLayer(polyline);
};
}, []);

return ruler;
}

function RulerMarker({ position, length }) {
return (

circle-small

`,
className: "marker-icon",
iconSize: [24, 24],
popupAnchor: [0, -12],
})}
>

{length.toFixed(3)} KM


);
}

export default Ruler;
```

## License

[MIT](https://choosealicense.com/licenses/mit/)

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):



Charles Richardson
Charles Richardson

💻
saiNaruUFL
saiNaruUFL

💻
Alejandro Ramírez Muñoz
Alejandro Ramírez Muñoz

💻

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!