Skip to content

Commit 8808094

Browse files
author
Bhaskar Karambelkar
committed
Auto show/hide legends with support for LayerControl and groupOptions. Replaces #400.
1 parent 783ae7e commit 8808094

File tree

6 files changed

+109
-16
lines changed

6 files changed

+109
-16
lines changed

R/legend.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
#' @export
6060
addLegend <- function(
6161
map, position = c('topright', 'bottomright', 'bottomleft', 'topleft'),
62-
pal, values, na.label = 'NA', bins = 7, colors, opacity = 0.5, labels,
62+
pal, values, na.label = 'NA', bins = 7, colors, opacity = 0.5, labels = NULL,
6363
labFormat = labelFormat(), title = NULL, className = "info legend",
6464
layerId = NULL, group = NULL
6565
) {

inst/examples/groupOptions.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
library(leaflet)
2+
pal <- colorQuantile("YlOrRd", quakes$mag)
3+
4+
leaflet(quakes) %>%
5+
addProviderTiles(providers$Esri.OceanBasemap, group = "basic") %>%
6+
addCircleMarkers(group = "detail", fillOpacity = 0.5,
7+
radius = ~mag * 5, color = ~pal(mag), stroke = FALSE) %>%
8+
addLegend(pal = pal, values = ~mag, group='detail', position='bottomleft')
9+
10+
l <- leaflet(quakes) %>%
11+
addProviderTiles(providers$Esri.OceanBasemap, group = "basic") %>%
12+
addMarkers(data = quakes, group = "basic") %>%
13+
addCircleMarkers(group = "detail", fillOpacity = 0.5,
14+
radius = ~mag * 5, color = ~pal(mag), stroke = FALSE) %>%
15+
addLegend(pal = pal, values = ~mag, group='detail', position='bottomleft') %>%
16+
groupOptions("detail", zoomLevels = 7:18) %>%
17+
addControl(htmltools::HTML("Zoom Level"), position = "topright",
18+
layerId = "zoom_display")
19+
20+
# Just to show the zoom level
21+
htmlwidgets::onRender(l, jsCode = htmlwidgets::JS(
22+
"function(el, x) {
23+
debugger;
24+
var map = this;
25+
detailsControl = document.getElementById('zoom_display');
26+
detailsControl.innerHTML = '<div>Zoom Level:'+map.getZoom()+'</div>';
27+
map.on('zoomend', function(e) {
28+
detailsControl = document.getElementById('zoom_display');
29+
detailsControl.innerHTML = '<div>Zoom Level:'+map.getZoom()+'</div>';
30+
});
31+
}"))

inst/htmlwidgets/leaflet.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ var ControlStore = function () {
9292
}
9393
this._map.addControl(control);
9494
}
95+
}, {
96+
key: "get",
97+
value: function get(id) {
98+
var control = null;
99+
if (this._controlsById[id]) {
100+
control = this._controlsById[id];
101+
}
102+
return control;
103+
}
95104
}, {
96105
key: "remove",
97106
value: function remove(id) {
@@ -1753,6 +1762,10 @@ methods.removeControl = function (layerId) {
17531762
this.controls.remove(layerId);
17541763
};
17551764

1765+
methods.getControl = function (layerId) {
1766+
this.controls.get(layerId);
1767+
};
1768+
17561769
methods.clearControls = function () {
17571770
this.controls.clear();
17581771
};
@@ -1876,6 +1889,16 @@ methods.addLegend = function (options) {
18761889
map.controls.remove(options.layerId);
18771890
}
18781891
});
1892+
map.on("groupadd", function (e) {
1893+
if (e.name === options.group) {
1894+
map.controls.add(legend, options.layerId);
1895+
}
1896+
});
1897+
map.on("groupremove", function (e) {
1898+
if (e.name === options.group) {
1899+
map.controls.remove(options.layerId);
1900+
}
1901+
});
18791902
})();
18801903
}
18811904

@@ -1968,9 +1991,15 @@ function setupShowHideGroupsOnZoom(map) {
19681991
}
19691992
map.leafletr._hasInitializedShowHideGroups = true;
19701993

1971-
function setVisibility(layer, visible) {
1994+
function setVisibility(layer, visible, group) {
19721995
if (visible !== map.hasLayer(layer)) {
1973-
if (visible) map.addLayer(layer);else map.removeLayer(layer);
1996+
if (visible) {
1997+
map.addLayer(layer);
1998+
map.fire("groupadd", { "name": group, "layer": layer });
1999+
} else {
2000+
map.removeLayer(layer);
2001+
map.fire("groupremove", { "name": group, "layer": layer });
2002+
}
19742003
}
19752004
}
19762005

@@ -1981,7 +2010,7 @@ function setupShowHideGroupsOnZoom(map) {
19812010
map.layerManager.getAllGroupNames().forEach(function (group) {
19822011
var layer = map.layerManager.getLayerGroup(group, false);
19832012
if (layer && typeof layer.zoomLevels !== "undefined") {
1984-
setVisibility(layer, layer.zoomLevels === true || layer.zoomLevels.indexOf(zoom) >= 0);
2013+
setVisibility(layer, layer.zoomLevels === true || layer.zoomLevels.indexOf(zoom) >= 0, group);
19852014
}
19862015
});
19872016
}
@@ -2371,6 +2400,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
23712400
// pixel of the original image has some contribution to the downscaled image)
23722401
// as opposed to a single-step downscaling which will discard a lot of data
23732402
// (and with sparse images at small scales can give very surprising results).
2403+
23742404
var Mipmapper = function () {
23752405
function Mipmapper(img) {
23762406
_classCallCheck(this, Mipmapper);

javascript/src/control-store.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ export default class ControlStore {
1818
this._map.addControl(control);
1919
}
2020

21+
get(id) {
22+
let control = null;
23+
if (this._controlsById[id]) {
24+
control = this._controlsById[id];
25+
}
26+
return control;
27+
}
28+
2129
remove(id) {
2230
if (this._controlsById[id]) {
2331
let control = this._controlsById[id];
@@ -39,4 +47,4 @@ export default class ControlStore {
3947
}
4048
this._controlsById = {};
4149
}
42-
}
50+
}

javascript/src/methods.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,10 @@ methods.removeControl = function(layerId) {
676676
this.controls.remove(layerId);
677677
};
678678

679+
methods.getControl = function(layerId) {
680+
this.controls.get(layerId);
681+
};
682+
679683
methods.clearControls = function() {
680684
this.controls.clear();
681685
};
@@ -811,6 +815,16 @@ methods.addLegend = function(options) {
811815
map.controls.remove(options.layerId);
812816
}
813817
});
818+
map.on("groupadd", function(e){
819+
if(e.name === options.group) {
820+
map.controls.add(legend, options.layerId);
821+
}
822+
});
823+
map.on("groupremove", function(e){
824+
if(e.name === options.group) {
825+
map.controls.remove(options.layerId);
826+
}
827+
});
814828
}
815829

816830
this.controls.add(legend, options.layerId);
@@ -897,12 +911,15 @@ function setupShowHideGroupsOnZoom(map) {
897911
}
898912
map.leafletr._hasInitializedShowHideGroups = true;
899913

900-
function setVisibility(layer, visible) {
914+
function setVisibility(layer, visible, group) {
901915
if (visible !== map.hasLayer(layer)) {
902-
if (visible)
916+
if (visible) {
903917
map.addLayer(layer);
904-
else
918+
map.fire("groupadd", {"name": group, "layer": layer});
919+
} else {
905920
map.removeLayer(layer);
921+
map.fire("groupremove", {"name": group, "layer": layer});
922+
}
906923
}
907924
}
908925

@@ -915,7 +932,8 @@ function setupShowHideGroupsOnZoom(map) {
915932
let layer = map.layerManager.getLayerGroup(group, false);
916933
if (layer && typeof(layer.zoomLevels) !== "undefined") {
917934
setVisibility(layer,
918-
layer.zoomLevels === true || layer.zoomLevels.indexOf(zoom) >= 0);
935+
layer.zoomLevels === true || layer.zoomLevels.indexOf(zoom) >= 0,
936+
group);
919937
}
920938
});
921939
}

man/addLegend.Rd

Lines changed: 13 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)