|
1 | | -# leaflet-polydiff |
| 1 | +# leaflet-polydiff |
| 2 | +> Leaflet layer for efficient data fetching. |
| 3 | +
|
| 4 | +`leaflet-polydiff` keeps track of user movements to compute the **difference between newly visited area in the map, and the sum of all previously visited area**, thanks to [Turf.js](https://turfjs.org). |
| 5 | + |
| 6 | +The resulting polygon can be used to make API calls to a backend, in order to optimize data fetching of GeoJSON feature. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +You can install `leaflet-polydiff` with NPM: |
| 11 | + |
| 12 | +```bash |
| 13 | +npm i @20tab/leaflet-polydiff |
| 14 | +``` |
| 15 | + |
| 16 | +Or with YARN: |
| 17 | + |
| 18 | +```bash |
| 19 | +yarn add @20tab/leaflet-polydiff |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +`leaflet-polydiff` is distributed as an ES2015 module, and is intended to be used with a module bundler. |
| 25 | + |
| 26 | +```typescript jsx |
| 27 | +import * as L from "leaflet"; |
| 28 | + |
| 29 | +import PolyDiff from "@20tab/leaflet-polydiff"; |
| 30 | + |
| 31 | +/** |
| 32 | + * Map initialization. |
| 33 | + */ |
| 34 | +const copy = |
| 35 | + "© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors"; |
| 36 | +const url = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"; |
| 37 | +const osm = L.tileLayer(url, { attribution: copy }); |
| 38 | + |
| 39 | +const leafletMap = L.map("map", { |
| 40 | + layers: [osm], |
| 41 | + zoom: 15, |
| 42 | + center: [43.71994, 10.948968], |
| 43 | +}); |
| 44 | + |
| 45 | +/** |
| 46 | + * PolyDiff activation. |
| 47 | + */ |
| 48 | + |
| 49 | +PolyDiff().addTo(leafletMap); |
| 50 | + |
| 51 | +leafletMap.on("polydiff", (event) => { |
| 52 | + // The event object will carry a diff polygon. |
| 53 | + event.diff; |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +## Props |
| 58 | + |
| 59 | +| Name | Type | Default | Description | |
| 60 | +|--------------|-----------------------------------------------------------------------|---------------|------------------------------------| |
| 61 | +| eventName? | string | "polydiff" | The event name for the diff event. | |
| 62 | +| pane? | [LayerOptions](https://leafletjs.com/reference.html#layer-pane) | "overlayPane" | See link. | |
| 63 | +| attribution? | [LayerOptions](https://leafletjs.com/reference.html#layer-pane) | null | See link. | |
0 commit comments