Skip to content

Commit

Permalink
wmslegend show/hide on layer/group-add/remove, add group arg
Browse files Browse the repository at this point in the history
  • Loading branch information
trafficonese committed Mar 23, 2024
1 parent caa679e commit 028d24e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
7 changes: 3 additions & 4 deletions R/wms-legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ wms.legendDependency <- function() {

#' Add WMS Legend
#' @description Add a WMS Legend
#' @param map The leaflet map
#' @param uri The legend URI
#' @param position position of control: "topleft", "topright", "bottomleft", or "bottomright"
#' @param layerId A unique ID for the Legend
#' @inheritParams leaflet:.addLegend
#' @rdname wms-legend
#' @export
#' @examples
Expand All @@ -30,10 +28,11 @@ wms.legendDependency <- function() {
#' "format=image/png&layer=Personenschaden_5000"
#' )
#' )
addWMSLegend <- function(map, uri, position = "topright", layerId = NULL) {
addWMSLegend <- function(map, uri, position = "topright", layerId = NULL, group = NULL) {
map$dependencies <- c(map$dependencies, wms.legendDependency())
options <- leaflet::filterNULL(list(
layerId = layerId,
group = group,
options = list(uri = uri, position = position)
))

Expand Down
34 changes: 30 additions & 4 deletions inst/htmlwidgets/bindings/lfx-wms-legend-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@ LeafletWidget.methods.addWMSLegend = function(options) {
(function() {
var map = this;
var wmsLegendControl = new L.Control.WMSLegend(options.options);
if (options.layerId) {
map.controls.add(wmsLegendControl, options.layerId);
} else {
map.controls.add(wmsLegendControl);

if (options.group) {
// Auto generate a layerID if not provided
if(!options.layerId) {
options.layerId = L.Util.stamp(wmsLegendControl);
}

map.on("overlayadd", function(e){
if(e.name === options.group) {
map.controls.add(wmsLegendControl, options.layerId);
}
});
map.on("overlayremove", function(e){
if(e.name === options.group) {
map.controls.remove(options.layerId);
}
});
map.on("groupadd", function(e){
if(e.name === options.group) {
map.controls.add(wmsLegendControl, options.layerId);
}
});
map.on("groupremove", function(e){
if(e.name === options.group) {
map.controls.remove(options.layerId);
}
});
}

map.controls.add(wmsLegendControl, options.layerId);

}).call(this);
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions man/wms-legend.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 028d24e

Please sign in to comment.