Skip to content

Commit

Permalink
Merge pull request #4 from gaorocks/master
Browse files Browse the repository at this point in the history
Add support for dynamic styling similar to L.geoJSON/Leaflet.VectorGrid
  • Loading branch information
iamtekson authored Jan 5, 2022
2 parents ba014a5 + 43e2d42 commit 2161255
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ var options = {
var vtLayer = L.geoJson.vt(geojson, options).addTo(map);
```

Apart from an `Object`, a `Function` can also be assigned to `options.style` in order to handle style dynamically.

```js
var options = {
maxZoom: 16,
tolerance: 3,
debug: 0,
style: (properties) => {
if (properties.ADM1_PCODE == 'NP07') {
return {fillColor:"#0F0",color:"#F2FF00"};
} else {
return {fillColor:"#1EB300",color:"#F2FF00"};
}
}
};
```

Options are included with [geojson-vt options](https://github.com/mapbox/geojson-vt#options) and [L.geojson style](http://leafletjs.com/reference.html#path-options).

The following are the default options from geojson-vt.
Expand Down
2 changes: 1 addition & 1 deletion src/leaflet-geojson-vt.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ L.GeoJSON.VT = L.GridLayer.extend({
var typeChanged = type !== feature.type,
type = feature.type;
ctx.beginPath();
if (this.options.style) this.setStyle(ctx, this.options.style);
if (this.options.style) this.options.style instanceof Function ? this.setStyle(ctx, this.options.style(feature.tags)) : this.setStyle(ctx, this.options.style);
if (type === 2 || type === 3) {
for (var j = 0; j < feature.geometry.length; j++) {
var ring = feature.geometry[j];
Expand Down

0 comments on commit 2161255

Please sign in to comment.