Skip to content

Commit 8ddee94

Browse files
committed
Add groupOptions, with the ability to set zoom levels
1 parent b35b573 commit 8ddee94

File tree

8 files changed

+186
-12
lines changed

8 files changed

+186
-12
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export(expandLimitsBbox)
107107
export(filterNULL)
108108
export(fitBounds)
109109
export(getMapData)
110+
export(groupOptions)
110111
export(hideGroup)
111112
export(highlightOptions)
112113
export(iconList)

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
leaflet 1.1.1
2+
--------------------------------------------------------------------------------
3+
4+
* Add `groupOptions` parameter, currently the only option is letting you specify
5+
zoom levels at which a group should be visible.
6+
17
leaflet 1.1.0
28
--------------------------------------------------------------------------------
39

R/layers.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,40 @@ hideGroup <- function(map, group) {
9898
invokeMethod(map, getMapData(map), 'hideGroup', group)
9999
}
100100

101+
#' Set options on layer groups
102+
#'
103+
#' Change options on layer groups. Currently the only option is to control what
104+
#' zoom levels a layer group will be displayed at. The \code{zoomLevels} option
105+
#' is not compatible with \link[=addLayersControl]{layers control}; do not both
106+
#' assign a group to zoom levels and use it with \code{addLayersControl}.
107+
#'
108+
#' @param map the map to modify
109+
#' @param group character vector of one or more group names to set options on
110+
#' @param zoomLevels numeric vector of zoom levels at which group(s) should be
111+
#' visible, or \code{TRUE} to display at all zoom levels
112+
#'
113+
#' @examples
114+
#' pal <- colorQuantile("YlOrRd", quakes$mag)
115+
#'
116+
#' leaflet() %>%
117+
#' # Basic markers
118+
#' addTiles(group = "basic") %>%
119+
#' addMarkers(data = quakes, group = "basic") %>%
120+
#' # When zoomed in, we'll show circles at the base of each marker whose
121+
#' # radius and color reflect the magnitude
122+
#' addProviderTiles(providers$Stamen.TonerLite, group = "detail") %>%
123+
#' addCircleMarkers(data = quakes, group = "detail", fillOpacity = 0.5,
124+
#' radius = ~mag * 5, color = ~pal(mag), stroke = FALSE) %>%
125+
#' # Set the detail group to only appear when zoomed in
126+
#' groupOptions("detail", zoomLevels = 7:18)
127+
#'
128+
#' @export
129+
groupOptions <- function(map, group, zoomLevels = NULL) {
130+
invokeMethod(map, getMapData(map), 'setGroupOptions', group,
131+
list(zoomLevels = zoomLevels)
132+
)
133+
}
134+
101135
#' Graphics elements and layers
102136
#'
103137
#' Add graphics elements and layers to the map widget.

inst/htmlwidgets/leaflet.js

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,30 @@ function preventUnintendedZoomOnScroll(map) {
496496
});
497497
}
498498

499+
function setupShowHideGroupsOnZoom(map) {
500+
function setVisibility(layer, visible) {
501+
if (visible !== map.hasLayer(layer)) {
502+
if (visible) map.addLayer(layer);else map.removeLayer(layer);
503+
}
504+
}
505+
506+
function showHideGroupsOnZoom() {
507+
if (!map.layerManager) return;
508+
509+
var zoom = map.getZoom();
510+
map.layerManager.getAllGroupNames().forEach(function (group) {
511+
var layer = map.layerManager.getLayerGroup(group, false);
512+
if (layer && typeof layer.zoomLevels !== "undefined") {
513+
setVisibility(layer, layer.zoomLevels === true || layer.zoomLevels.indexOf(zoom) >= 0);
514+
}
515+
});
516+
}
517+
518+
map.showHideGroupsOnZoom = showHideGroupsOnZoom;
519+
map.on("zoomend", showHideGroupsOnZoom);
520+
showHideGroupsOnZoom();
521+
}
522+
499523
_htmlwidgets2.default.widget({
500524

501525
name: "leaflet",
@@ -534,6 +558,7 @@ _htmlwidgets2.default.widget({
534558
}
535559

536560
preventUnintendedZoomOnScroll(map);
561+
setupShowHideGroupsOnZoom(map);
537562

538563
// Store some state in the map object
539564
map.leafletr = {
@@ -1023,6 +1048,15 @@ var LayerManager = function () {
10231048
});
10241049
return result;
10251050
}
1051+
}, {
1052+
key: "getAllGroupNames",
1053+
value: function getAllGroupNames() {
1054+
var result = [];
1055+
_jquery2.default.each(this._groupContainers, function (k, v) {
1056+
result.push(k);
1057+
});
1058+
return result;
1059+
}
10261060
}, {
10271061
key: "clearGroup",
10281062
value: function clearGroup(group) {
@@ -1930,6 +1964,18 @@ methods.showGroup = function (group) {
19301964
});
19311965
};
19321966

1967+
methods.setGroupOptions = function (group, options) {
1968+
var _this8 = this;
1969+
1970+
_jquery2.default.each((0, _util.asArray)(group), function (i, g) {
1971+
var layer = _this8.layerManager.getLayerGroup(g, true);
1972+
if (options.zoomLevels) {
1973+
layer.zoomLevels = options.zoomLevels;
1974+
}
1975+
});
1976+
this.showHideGroupsOnZoom();
1977+
};
1978+
19331979
methods.addRasterImage = function (uri, bounds, opacity, attribution, layerId, group) {
19341980
// uri is a data URI containing an image. We want to paint this image as a
19351981
// layer at (top-left) bounds[0] to (bottom-right) bounds[1].
@@ -2213,7 +2259,7 @@ methods.removeMeasure = function () {
22132259
};
22142260

22152261
methods.addSelect = function (ctGroup) {
2216-
var _this8 = this;
2262+
var _this9 = this;
22172263

22182264
methods.removeSelect.call(this);
22192265

@@ -2224,42 +2270,42 @@ methods.addSelect = function (ctGroup) {
22242270
title: "Make a selection",
22252271
onClick: function onClick(btn, map) {
22262272
btn.state("select-active");
2227-
_this8._locationFilter = new _leaflet2.default.LocationFilter2();
2273+
_this9._locationFilter = new _leaflet2.default.LocationFilter2();
22282274

22292275
if (ctGroup) {
22302276
(function () {
22312277
var selectionHandle = new global.crosstalk.SelectionHandle(ctGroup);
22322278
selectionHandle.on("change", function (e) {
22332279
if (e.sender !== selectionHandle) {
2234-
if (_this8._locationFilter) {
2235-
_this8._locationFilter.disable();
2280+
if (_this9._locationFilter) {
2281+
_this9._locationFilter.disable();
22362282
btn.state("select-inactive");
22372283
}
22382284
}
22392285
});
22402286
var handler = function handler(e) {
2241-
_this8.layerManager.brush(_this8._locationFilter.getBounds(), { sender: selectionHandle });
2287+
_this9.layerManager.brush(_this9._locationFilter.getBounds(), { sender: selectionHandle });
22422288
};
2243-
_this8._locationFilter.on("enabled", handler);
2244-
_this8._locationFilter.on("change", handler);
2245-
_this8._locationFilter.on("disabled", function () {
2289+
_this9._locationFilter.on("enabled", handler);
2290+
_this9._locationFilter.on("change", handler);
2291+
_this9._locationFilter.on("disabled", function () {
22462292
selectionHandle.close();
2247-
_this8._locationFilter = null;
2293+
_this9._locationFilter = null;
22482294
});
22492295
})();
22502296
}
22512297

2252-
_this8._locationFilter.addTo(map);
2298+
_this9._locationFilter.addTo(map);
22532299
}
22542300
}, {
22552301
stateName: "select-active",
22562302
icon: "ion-close-round",
22572303
title: "Dismiss selection",
22582304
onClick: function onClick(btn, map) {
22592305
btn.state("select-inactive");
2260-
_this8._locationFilter.disable();
2306+
_this9._locationFilter.disable();
22612307
// If explicitly dismissed, clear the crosstalk selections
2262-
_this8.layerManager.unbrush();
2308+
_this9.layerManager.unbrush();
22632309
}
22642310
}]
22652311
});

javascript/src/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,35 @@ function preventUnintendedZoomOnScroll(map) {
7979
});
8080
}
8181

82+
function setupShowHideGroupsOnZoom(map) {
83+
function setVisibility(layer, visible) {
84+
if (visible !== map.hasLayer(layer)) {
85+
if (visible)
86+
map.addLayer(layer);
87+
else
88+
map.removeLayer(layer);
89+
}
90+
}
91+
92+
function showHideGroupsOnZoom() {
93+
if (!map.layerManager)
94+
return;
95+
96+
let zoom = map.getZoom();
97+
map.layerManager.getAllGroupNames().forEach(group => {
98+
let layer = map.layerManager.getLayerGroup(group, false);
99+
if (layer && typeof(layer.zoomLevels) !== "undefined") {
100+
setVisibility(layer,
101+
layer.zoomLevels === true || layer.zoomLevels.indexOf(zoom) >= 0);
102+
}
103+
});
104+
}
105+
106+
map.showHideGroupsOnZoom = showHideGroupsOnZoom;
107+
map.on("zoomend", showHideGroupsOnZoom);
108+
showHideGroupsOnZoom();
109+
}
110+
82111
HTMLWidgets.widget({
83112

84113
name: "leaflet",
@@ -115,6 +144,7 @@ HTMLWidgets.widget({
115144
}
116145

117146
preventUnintendedZoomOnScroll(map);
147+
setupShowHideGroupsOnZoom(map);
118148

119149
// Store some state in the map object
120150
map.leafletr = {

javascript/src/layer-manager.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ export default class LayerManager {
282282
return result;
283283
}
284284

285+
getAllGroupNames() {
286+
let result = [];
287+
$.each(this._groupContainers, (k, v) => {
288+
result.push(k);
289+
});
290+
return result;
291+
}
292+
285293
clearGroup(group) {
286294
// Find all layers in _byGroup[group]
287295
let groupTable = this._byGroup[group];

javascript/src/methods.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,16 @@ methods.showGroup = function(group) {
871871
});
872872
};
873873

874+
methods.setGroupOptions = function(group, options) {
875+
$.each(asArray(group), (i, g) => {
876+
let layer = this.layerManager.getLayerGroup(g, true);
877+
if (options.zoomLevels) {
878+
layer.zoomLevels = options.zoomLevels;
879+
}
880+
});
881+
this.showHideGroupsOnZoom();
882+
};
883+
874884
methods.addRasterImage = function(uri, bounds, opacity, attribution, layerId, group) {
875885
// uri is a data URI containing an image. We want to paint this image as a
876886
// layer at (top-left) bounds[0] to (bottom-right) bounds[1].

man/groupOptions.Rd

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

0 commit comments

Comments
 (0)