Skip to content

Commit 0391800

Browse files
committed
Add visible attribute to GeoJSON layer
1 parent a19fbca commit 0391800

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

examples/CountriesGeoJSON.ipynb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@
5050
"execution_count": null,
5151
"metadata": {},
5252
"outputs": [],
53-
"source": []
53+
"source": [
54+
"layer.visible = False"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"layer.visible = True"
64+
]
5465
}
5566
],
5667
"metadata": {
@@ -69,7 +80,7 @@
6980
"name": "python",
7081
"nbconvert_exporter": "python",
7182
"pygments_lexer": "ipython3",
72-
"version": "3.9.7"
83+
"version": "3.10.5"
7384
}
7485
},
7586
"nbformat": 4,

ipyleaflet/leaflet.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ class GeoJSON(FeatureGroup):
12361236

12371237
data = Dict().tag(sync=True)
12381238
style = Dict().tag(sync=True)
1239+
visible = Bool(True).tag(sync=True)
12391240
hover_style = Dict().tag(sync=True)
12401241
point_style = Dict().tag(sync=True)
12411242
style_callback = Any()

js/src/layers/GeoJSON.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class LeafletGeoJSONModel extends featuregroup.LeafletFeatureGroupModel {
1212
_model_name: 'LeafletGeoJSONModel',
1313
data: {},
1414
style: {},
15+
visible: true,
1516
hover_style: {},
1617
point_style: {}
1718
};
@@ -82,5 +83,17 @@ export class LeafletGeoJSONView extends featuregroup.LeafletFeatureGroupView {
8283
},
8384
this
8485
);
86+
this.listenTo(
87+
this.model,
88+
'change:visible',
89+
function() {
90+
if (this.model.get('visible')) {
91+
this.obj.addData(this.model.get('data'));
92+
} else {
93+
this.obj.clearLayers();
94+
}
95+
},
96+
this
97+
);
8598
}
8699
}

0 commit comments

Comments
 (0)