Skip to content

Commit 3ea0f3b

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

File tree

1 file changed

+59
-12
lines changed

1 file changed

+59
-12
lines changed

README.md

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Or use it from CDN
4242
> Set your GeoJSON source with `clusterMaxZoom: 22` in order to let the plugin handle individual marker rendering at the highest zoom level.
4343
4444
```javascript
45-
import { TeritorioCluster } from '@teritorio/maplibre-gl-teritorio-cluster'
45+
import { buildCss, TeritorioCluster } from '@teritorio/maplibre-gl-teritorio-cluster'
4646
import maplibregl from 'maplibre-gl'
4747
import 'maplibre-gl/dist/maplibre-gl.css'
4848

@@ -80,11 +80,60 @@ const map = new maplibregl.Map({
8080
}
8181
})
8282

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

90139
// Add the layer to map
@@ -95,15 +144,6 @@ map.on('load', () => {
95144
console.log(event.detail.selectedFeature)
96145
})
97146
})
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) {}
107147
```
108148

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

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

140187
```js
141188
clusterLayer.addEventListener('feature-click', (event) => {
142-
console.log('Selected feature:', event.detail.selectedFeature)
189+
console.info('Selected feature:', event.detail.selectedFeature)
143190
})
144191
```
145192
## Run Locally

0 commit comments

Comments
 (0)