Skip to content

Commit 7fdfe20

Browse files
committed
docs: separate rendering function + add examples #38
1 parent 710315a commit 7fdfe20

File tree

1 file changed

+67
-13
lines changed

1 file changed

+67
-13
lines changed

README.md

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Enhance MapLibre GL JS with fully interactive HTML-based clusters and markers.
1010
**3 Cluster States:**
1111
- Cluster Marker: A single marker representing a cluster, eg. displaying the number of features.
1212
- Unfolded Cluster: Displays individual features grouped from a small cluster or when at the highest zoom level.
13-
- Unclustered Features
13+
- Unclustered Features
1414

1515
**Customization Callbacks:**
1616
- Custom function to render cluster.
@@ -23,6 +23,7 @@ Enhance MapLibre GL JS with fully interactive HTML-based clusters and markers.
2323
👉 [**Live Demo**](https://teritorio.github.io/maplibre-gl-teritorio-cluster/index.html)
2424

2525
![Demo screenshot](public/image.png)
26+
2627
## Installation
2728

2829
Install @teritorio/maplibre-gl-teritorio-cluster with yarn
@@ -42,7 +43,7 @@ Or use it from CDN
4243
> Set your GeoJSON source with `clusterMaxZoom: 22` in order to let the plugin handle individual marker rendering at the highest zoom level.
4344
4445
```javascript
45-
import { TeritorioCluster } from '@teritorio/maplibre-gl-teritorio-cluster'
46+
import { buildCss, TeritorioCluster } from '@teritorio/maplibre-gl-teritorio-cluster'
4647
import maplibregl from 'maplibre-gl'
4748
import 'maplibre-gl/dist/maplibre-gl.css'
4849

@@ -80,11 +81,60 @@ const map = new maplibregl.Map({
8081
}
8182
})
8283

84+
// Create whatever HTML element you want as Cluster
85+
function clusterRender(element, props) {
86+
element.innerHTML = props.point_count.toLocaleString()
87+
element.style.setProperty('background-color', 'white')
88+
element.style.setProperty('border-radius', '100%')
89+
element.style.setProperty('border', '2px solid green')
90+
element.style.setProperty('justify-content', 'center')
91+
element.style.setProperty('align-items', 'center')
92+
element.style.setProperty('display', 'flex')
93+
element.style.setProperty('color', 'black')
94+
element.style.setProperty('width', '60px')
95+
element.style.setProperty('height', '60px')
96+
element.style.setProperty('cursor', 'pointer')
97+
}
98+
99+
// Create whatever HTML element you want as individual Marker
100+
// You can you buildCss helper as well (exported by @teritorio/maplibre-gl-teritorio-cluster)
101+
function markerRender(element, feature, markerSize) {
102+
element.innerHTML = props.point_count.toLocaleString()
103+
104+
buildCss(element, {
105+
'background-color': 'white',
106+
'border-radius': '100%',
107+
'border': '2px solid green',
108+
'justify-content': 'center',
109+
'align-items': 'center',
110+
'display': 'flex',
111+
'color': 'black',
112+
'width': `60px`,
113+
'height': `60px`,
114+
'cursor': 'pointer'
115+
})
116+
}
117+
118+
// Create whatever HTML element you want as Pin Marker
119+
function pinMarkerRender(coords, offset) {
120+
return new maplibregl.Marker({
121+
scale: 1.3,
122+
color: '#f44336',
123+
anchor: 'bottom'
124+
})
125+
.setLngLat(coords)
126+
.setOffset(offset)
127+
}
128+
83129
map.on('load', () => {
84130
const clusterLayer = new TeritorioCluster(
85131
'teritorio-cluster-layer',
86132
'points',
87-
options
133+
{
134+
clusterRender,
135+
markerRender,
136+
pinMarkerRender
137+
}
88138
)
89139

90140
// Add the layer to map
@@ -95,15 +145,6 @@ map.on('load', () => {
95145
console.log(event.detail.selectedFeature)
96146
})
97147
})
98-
99-
// Create whatever HTML element you want as Cluster
100-
function clusterRender(element, props) {}
101-
102-
// Create whatever HTML element you want as individual Marker
103-
function markerRender(element, feature, markerSize) {}
104-
105-
// Create whatever HTML element you want as Pin Marker
106-
function pinMarkerRender(coords, offset) {}
107148
```
108149

109150
## API Reference
@@ -135,11 +176,18 @@ const clusterLayer = new TeritorioCluster(id, sourceId, options)
135176
| `initialFeature` | `GeoJSONFeature \| undefined` | `undefined` | Feature to auto-select on load. |
136177
| `pinMarkerRender` | `(feature) => HTMLElement` | `pinMarkerRenderDefault` | Custom renderer for the pinned marker. |
137178

179+
##### unfoldedClusterRender
180+
The following rendering function are at your disposal:
181+
- `unfoldedClusterRenderCircle`: Renders an unfolded cluster in a circular shape.
182+
- `unfoldedClusterRenderHexaGrid`: Renders an unfolded cluster in a hexagonal grid spiral.
183+
- `unfoldedClusterRenderGrid`: Renders an unfolded cluster in a square grid.
184+
- `unfoldedClusterRenderSmart` (default): Renders an unfolded cluster in a smart shape based on the number of items.
185+
138186
## Events
139187

140188
```js
141189
clusterLayer.addEventListener('feature-click', (event) => {
142-
console.log('Selected feature:', event.detail.selectedFeature)
190+
console.info('Selected feature:', event.detail.selectedFeature)
143191
})
144192
```
145193
## Run Locally
@@ -168,6 +216,12 @@ Start the server
168216
yarn dev
169217
```
170218

219+
## Peer Dependencies
220+
221+
This library requires the following peer dependencies to be installed in your project:
222+
223+
- [maplibre-gl-js](https://github.com/maplibre/maplibre-gl-js) >= v5.4.0
224+
171225
## Contributing
172226

173227
Contributions are always welcome!

0 commit comments

Comments
 (0)