|
| 1 | +#' Add additional panes to leaflet map to control layer order |
| 2 | +#' |
| 3 | +#' @description |
| 4 | +#' map panes can be created by supplying a name and a zIndex to control layer |
| 5 | +#' ordering. We recommend a \code{zIndex} value between 400 (the default |
| 6 | +#' overlay pane) and 500 (the default shadow pane). You can then use this pane |
| 7 | +#' to render overlays (points, lines, polygons) by setting the \code{pane} |
| 8 | +#' argument in \code{leafletOptions}. This will give you control |
| 9 | +#' over the order of the layers, e.g. points always on top of polygons. |
| 10 | +#' If two layers are provided to the same pane, overlay will be determined by |
| 11 | +#' order of adding. See examples below. |
| 12 | +#' See \url{http://www.leafletjs.com/reference-1.3.0.html#map-pane} for details. |
| 13 | +#' |
| 14 | +#' If the error "Cannot read property 'appendChild' of undefined" occurs, make |
| 15 | +#' sure the pane being used for used for display has already been added to the map. |
| 16 | +#' |
| 17 | +#' @param map A \code{leaflet} or \code{mapview} object. |
| 18 | +#' @param name The name of the new pane (refer to this in \code{leafletOptions}. |
| 19 | +#' @param zIndex The zIndex of the pane. Panes with higher index are rendered |
| 20 | +#' above panes with lower indices. |
| 21 | +#' |
| 22 | +#' @export |
| 23 | +#' @examples |
| 24 | +#' \donttest{rand_lng <- function(n = 10) rnorm(n, -93.65, .01) |
| 25 | +#' rand_lat <- function(n = 10) rnorm(n, 42.0285, .01) |
| 26 | +#' |
| 27 | +#' random_data <- data.frame( |
| 28 | +#' lng = rand_lng(50), |
| 29 | +#' lat = rand_lat(50), |
| 30 | +#' radius = runif(50, 50, 150), |
| 31 | +#' circleId = paste0("circle #", 1:50), |
| 32 | +#' lineId = paste0("circle #", 1:50) |
| 33 | +#' ) |
| 34 | +#' |
| 35 | +#' # display circles (zIndex: 420) above the lines (zIndex: 410), even when added first |
| 36 | +#' leaflet() %>% |
| 37 | +#' addTiles() %>% |
| 38 | +#' # move the center to Snedecor Hall |
| 39 | +#' setView(-93.65, 42.0285, zoom = 14) %>% |
| 40 | +#' addMapPane("ames_lines", zIndex = 410) %>% # shown below ames_circles |
| 41 | +#' addMapPane("ames_circles", zIndex = 420) %>% # shown above ames_lines |
| 42 | +#' # points above polygons |
| 43 | +#' addCircles( |
| 44 | +#' data = random_data, ~lng, ~lat, radius = ~radius, popup = ~circleId, |
| 45 | +#' options = pathOptions(pane = "ames_circles") |
| 46 | +#' ) %>% |
| 47 | +#' # lines in 'ames_lines' pane |
| 48 | +#' addPolylines( |
| 49 | +#' data = random_data, ~lng, ~lat, color = "#F00", weight = 20, |
| 50 | +#' options = pathOptions(pane = "ames_lines") |
| 51 | +#' ) |
| 52 | +#' |
| 53 | +#' |
| 54 | +#' # same example but circles (zIndex: 420) are below the lines (zIndex: 430) |
| 55 | +#' leaflet() %>% |
| 56 | +#' addTiles() %>% |
| 57 | +#' # move the center to Snedecor Hall |
| 58 | +#' setView(-93.65, 42.0285, zoom = 14) %>% |
| 59 | +#' addMapPane("ames_lines", zIndex = 430) %>% # shown below ames_circles |
| 60 | +#' addMapPane("ames_circles", zIndex = 420) %>% # shown above ames_lines |
| 61 | +#' # points above polygons |
| 62 | +#' addCircles( |
| 63 | +#' data = random_data, ~lng, ~lat, radius = ~radius, popup = ~circleId, |
| 64 | +#' options = pathOptions(pane = "ames_circles") |
| 65 | +#' ) %>% |
| 66 | +#' # lines in 'ames_lines' pane |
| 67 | +#' addPolylines( |
| 68 | +#' data = random_data, ~lng, ~lat, color = "#F00", weight = 20, |
| 69 | +#' options = pathOptions(pane = "ames_lines") |
| 70 | +#' ) |
| 71 | +#'} |
| 72 | +addMapPane = function(map, name, zIndex) { |
| 73 | + invokeMethod(map, getMapData(map), 'createMapPane', name, zIndex) |
| 74 | +} |
0 commit comments