Skip to content

Commit

Permalink
Vector tile layer opacity and visible
Browse files Browse the repository at this point in the history
  • Loading branch information
lopezvoliver committed Jun 25, 2024
1 parent 52f4cb7 commit 0cd034c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion python/ipyleaflet/ipyleaflet/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,10 @@ class VectorTileLayer(Layer):
Minimum zoom number the tile source has available. If it is specified, the tiles on all zoom levels lower than min_native_zoom will be loaded from min_native_zoom level and auto-scaled.
max_native_zoom: int, default None
Maximum zoom number the tile source has available. If it is specified, the tiles on all zoom levels higher than max_native_zoom will be loaded from max_native_zoom level and auto-scaled.
opacity: float, default 1.
Opacity of the layer between 0. (fully transparent) and 1. (fully opaque).
visible: boolean, default True
Whether the layer is visible or not.
"""

_view_name = Unicode("LeafletVectorTileLayerView").tag(sync=True)
Expand All @@ -1115,7 +1119,8 @@ class VectorTileLayer(Layer):
attribution = Unicode().tag(sync=True, o=True)

vector_tile_layer_styles = Union([Dict(), Unicode()]).tag(sync=True, o=True)

opacity = Float(1.0, min=0.0, max=1.0).tag(sync=True)
visible = Bool(True).tag(sync=True)
min_zoom = Int(0).tag(sync=True, o=True)
max_zoom = Int(18).tag(sync=True, o=True)
min_native_zoom = Int(default_value=None, allow_none=True).tag(sync=True, o=True)
Expand Down
19 changes: 17 additions & 2 deletions python/jupyter_leaflet/src/layers/VectorTileLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export class LeafletVectorTileLayerModel extends LeafletLayerModel {
max_zoom: 18,
min_native_zoom: null,
max_native_zoom: null,
interactive: true,
visible: true,
opacity: 1.0,
};
}
}
Expand All @@ -29,8 +32,8 @@ export class LeafletVectorTileLayerView extends LeafletLayerView {
...this.get_options(),
};
options['rendererFactory'] = L.canvas.tile;

let x: any = this.model.get('vectorTileLayerStyles');
let x: any = options['vectorTileLayerStyles'];
if (typeof x === 'string') {
try {
let blobCode = `const jsStyle=${x}; export { jsStyle };`;
Expand All @@ -55,6 +58,18 @@ export class LeafletVectorTileLayerView extends LeafletLayerView {
this.listenTo(this.model, 'change:url', () => {
this.obj.setUrl(this.model.get('url'));
});
this.listenTo(this.model, 'change:opacity', () => {
if (this.model.get('visible')) {
this.obj.setOpacity(this.model.get('opacity'));
}
});
this.listenTo(this.model, 'change:visible', () => {
if (this.model.get('visible')) {
this.obj.setOpacity(this.model.get('opacity'));
} else {
this.obj.setOpacity(0);
}
});
}

handle_message(content: { msg: string }) {
Expand Down

0 comments on commit 0cd034c

Please sign in to comment.