Skip to content

Commit a11aa05

Browse files
committed
Add setStyle()
1 parent c3fd1c1 commit a11aa05

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export(renderLeaflet)
155155
export(safeLabel)
156156
export(scaleBarOptions)
157157
export(setMaxBounds)
158+
export(setStyle)
158159
export(setView)
159160
export(showGroup)
160161
export(tileOptions)

R/methods.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ setView <- function(map, lng, lat, zoom, options = list()) {
3232
)
3333
}
3434

35+
#' Efficiently style a group that has already been added to the map
36+
#'
37+
#' Call with a group and a vector of style lists of length N. The first N
38+
#' features of the group will be restyled.
39+
#'
40+
#' @examples
41+
#' \donttest{
42+
#' renderLeaflet("map", {
43+
#' leaflet() %>% addPolygons(data = zones, group = "zones", color = "red")
44+
#' })
45+
#' colour = "blue"
46+
#' styles = lapply(pal(values), function(colour) {list(fillColor=colour, color=colour)})
47+
#' leafletProxy("map") %>%
48+
#' setStyle("zones", styles)
49+
#' }
50+
#' @export
51+
setStyle = function(map, group, styles) {
52+
invokeMethod(map, NULL, "setStyle", group, styles)
53+
}
54+
3555
#' @describeIn map-methods Flys to a given location/zoom-level using smooth pan-zoom.
3656
#' @export
3757
flyTo <- function(map, lng, lat, zoom, options = list()) {

inst/htmlwidgets/leaflet.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,15 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
12821282
var methods = {};
12831283
exports.default = methods;
12841284

1285+
/** Much more performant way to style loaded geometry */
1286+
1287+
methods.setStyle = function (group, styles) {
1288+
window.map = this;
1289+
var layers = this.layerManager.getLayerGroup(group).getLayers();
1290+
for (var i = 0; i < styles.length; i++) {
1291+
layers[i].setStyle(styles[i]);
1292+
}
1293+
};
12851294

12861295
function mouseHandler(mapId, layerId, group, eventName, extraInfo) {
12871296
return function (e) {

javascript/src/methods.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import Mipmapper from "./mipmapper";
1313
let methods = {};
1414
export default methods;
1515

16+
/** Much more performant way to style loaded geometry */
17+
methods.setStyle = function(group, styles) {
18+
window.map = this;
19+
let layers = this.layerManager.getLayerGroup(group).getLayers();
20+
for (let i = 0; i < styles.length; i++) {
21+
layers[i].setStyle(styles[i]);
22+
}
23+
};
1624

1725
function mouseHandler(mapId, layerId, group, eventName, extraInfo) {
1826
return function(e) {

man/setStyle.Rd

Lines changed: 23 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)