Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add visibility to MarkerCluster #1150

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ipyleaflet/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,8 @@ class MarkerCluster(Layer):
_model_name = Unicode('LeafletMarkerClusterModel').tag(sync=True)

markers = Tuple().tag(trait=Instance(Layer), sync=True, **widget_serialization)
visible = Bool(True).tag(sync=True)

# Options
disable_clustering_at_zoom = Int(18).tag(sync=True, o=True)
max_cluster_radius = Int(80).tag(sync=True, o=True)
Expand Down
4 changes: 4 additions & 0 deletions js/src/jupyter-leaflet.css
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,7 @@ applied to the map container */
border-radius: 50%;
-webkit-mask-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAA5JREFUeNpiYGBgAAgwAAAEAAGbA+oJAAAAAElFTkSuQmCC);
}

.test-icon {
opacity: 1;
}
49 changes: 48 additions & 1 deletion js/src/layers/MarkerCluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ export class LeafletMarkerClusterView extends layer.LeafletLayerView {
},
this
);
this.listenTo(
this.model,
'change:opacity',
function () {
if (this.model.get('visible')) {
document.querySelector('.test-icon').style.opacity =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not work if you have multiple marker clusters on the map and want to hide one and not the other. Hiding one would hide the other and that's unfortunately not what you want.

this.model.get('opacity');
}
},
this
);
this.listenTo(
this.model,
'change:visible',
function () {
if (this.model.get('visible')) {
document.querySelector('.test-icon').style.opacity =
this.model.get('opacity');
} else {
document.querySelector('.test-icon').style.opacity = 0;
}
},
this
);
}

remove_layer_view(child_view) {
Expand All @@ -52,12 +76,35 @@ export class LeafletMarkerClusterView extends layer.LeafletLayerView {

create_obj() {
var options = this.get_options();
this.obj = L.markerClusterGroup(options);
this.obj = L.markerClusterGroup({
disableClusteringAtZoom: options['disableClusteringAtZoom'],
maxClusterRadius: options['maxClusterRadius'],
Comment on lines +79 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing options manually you can unroll all of them with the following

Suggested change
this.obj = L.markerClusterGroup({
disableClusteringAtZoom: options['disableClusteringAtZoom'],
maxClusterRadius: options['maxClusterRadius'],
this.obj = L.markerClusterGroup({
...options,

iconCreateFunction: function (cluster) {
var childCount = cluster.getChildCount();

var c = ' marker-cluster-';
if (childCount < 10) {
c += 'small';
} else if (childCount < 100) {
c += 'medium';
} else {
c += 'large';
}

return new L.DivIcon({
html: '<div><span>' + childCount + '</span></div>',
className: 'test-icon marker-cluster' + c,
iconSize: new L.Point(40, 40),
});
},
});

this.marker_views = new widgets.ViewList(
this.add_layer_model,
this.remove_layer_view,
this
);

this.marker_views.update(this.model.get('markers'));
}
}