Skip to content

Commit b6c980d

Browse files
committed
marker cluster upgraded to latest version + support for freezing and layers.
1 parent e35c453 commit b6c980d

14 files changed

+3454
-19
lines changed

R/layers.R

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,9 @@ markerClusterDependencies = function() {
550550
list(
551551
htmltools::htmlDependency(
552552
'leaflet-markercluster',
553-
'0.4.0',
553+
'0.5.0',
554554
system.file('htmlwidgets/plugins/Leaflet.markercluster', package = 'leaflet'),
555-
script = 'leaflet.markercluster.js',
555+
script = c('leaflet.markercluster.js', 'leaflet.markercluster.layersupport-src.js', 'leaflet.markercluster.freezable-src.js'),
556556
stylesheet = c('MarkerCluster.css', 'MarkerCluster.Default.css')
557557
)
558558
)
@@ -747,17 +747,26 @@ markerOptions = function(
747747
#' spiderfy it so you can see all of its markers
748748
#' @param removeOutsideVisibleBounds clusters and markers too far from the
749749
#' viewport are removed from the map for performance
750+
#' @param spiderLegPolylineOptions Allows you to specify PolylineOptions (\url{http://leafletjs.com/reference.html#polyline-options}) to style spider legs. By default, they are { weight: 1.5, color: '#222', opacity: 0.5 }
751+
#' @param freezeAtZoom Allows you to freeze cluster expansion to a zoom level.
752+
#' Can be a zoom level e.g. 10, 12 or 'max' or 'maxKeepSpiderify'
753+
#' See \url{https://github.com/ghybs/Leaflet.MarkerCluster.Freezable#api-reference}
750754
#' @describeIn map-options Options for marker clusters
751755
#' @export
752756
markerClusterOptions = function(
753757
showCoverageOnHover = TRUE, zoomToBoundsOnClick = TRUE,
754-
spiderfyOnMaxZoom = TRUE, removeOutsideVisibleBounds = TRUE, ...
758+
spiderfyOnMaxZoom = TRUE, removeOutsideVisibleBounds = TRUE,
759+
spiderLegPolylineOptions = list(weight= 1.5, color= '#222', opacity= 0.5),
760+
freezeAtZoom = FALSE,
761+
...
755762
) {
756763
list(
757764
showCoverageOnHover = showCoverageOnHover,
758765
zoomToBoundsOnClick = zoomToBoundsOnClick,
759766
spiderfyOnMaxZoom = spiderfyOnMaxZoom,
760-
removeOutsideVisibleBounds = removeOutsideVisibleBounds, ...
767+
removeOutsideVisibleBounds = removeOutsideVisibleBounds,
768+
spiderLegPolylineOptions = spiderLegPolylineOptions,
769+
freezeAtZoom = freezeAtZoom, ...
761770
)
762771
}
763772

R/plugin-minimap.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ leafletMiniMapDependencies <- function() {
2929
#' @param zoomLevelFixed Overrides the offset to apply a fixed zoom level to
3030
#' the minimap regardless of the main map zoom.
3131
#' Set it to any valid zoom level, if unset zoomLevelOffset is used instead.
32-
#' @param centerFixed: Applies a fixed position to the minimap regardless of
32+
#' @param centerFixed Applies a fixed position to the minimap regardless of
3333
#' the main map's view / position. Prevents panning the minimap, but does
3434
#' allow zooming (both in the minimap and the main map).
3535
#' If the minimap is zoomed, it will always zoom around the centerFixed point.

inst/examples/marker-clustering.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
library(leaflet)
2+
3+
#' Add a Level factor to quakes
4+
quakes <- quakes %>%
5+
dplyr::mutate(mag.level = cut(mag,c(3,4,5,6),
6+
labels = c('>3 & <=4', '>4 & <=5', '>5 & <=6')))
7+
8+
l <- leaflet() %>% addTiles()
9+
10+
#' <br/><br/>
11+
#' Default Clustering
12+
l %>%
13+
addMarkers(data=quakes, clusterOptions = markerClusterOptions())
14+
15+
#' <br/><br/>
16+
#' Clustering Frozen at level 5
17+
l %>%
18+
addMarkers(data=quakes, clusterOptions = markerClusterOptions(freezeAtZoom=6))
19+
20+
#' <br/><br/>
21+
#' Clustering of Label Only Clusters
22+
l %>%
23+
addLabelOnlyMarkers(data=quakes,
24+
lng=~long, lat=~lat,
25+
label=~as.character(mag),
26+
clusterOptions = markerClusterOptions(),
27+
labelOptions = labelOptions(noHide = T,
28+
direction = 'auto'))
29+
#' <br/><br/>
30+
#' Clustering + Layers
31+
quakes.df <- split(quakes, quakes$mag.level)
32+
33+
l2 <- l
34+
names(quakes.df) %>%
35+
purrr::walk( function(df) {
36+
l2 <<- l2 %>%
37+
addMarkers(data=quakes.df[[df]],
38+
lng=~long, lat=~lat,
39+
label=~as.character(mag),
40+
popup=~as.character(mag),
41+
group = df,
42+
clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F),
43+
labelOptions = labelOptions(noHide = T,
44+
direction = 'auto'))
45+
})
46+
47+
l2 %>%
48+
addLayersControl(
49+
overlayGroups = names(quakes.df),
50+
options = layersControlOptions(collapsed = FALSE)
51+
)
52+
53+
#' <br/><br/>
54+
#' Clustering with custom iconCreateFunction
55+
leaflet(quakes) %>% addTiles() %>%
56+
addMarkers(clusterOptions =
57+
markerClusterOptions(iconCreateFunction =
58+
JS("
59+
function(cluster) {
60+
return new L.DivIcon({
61+
html: '<div style=\"background-color:rgba(77,77,77,0.5)\"><span>' + cluster.getChildCount() + '</div><span>',
62+
className: 'marker-cluster'
63+
});
64+
}")))

inst/htmlwidgets/leaflet.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,13 @@ function addMarkers(map, df, group, clusterOptions, clusterId, markerFunc) {
986986
var clusterGroup = this.layerManager.getLayer("cluster", clusterId),
987987
cluster = clusterOptions !== null;
988988
if (cluster && !clusterGroup) {
989-
clusterGroup = _leaflet2.default.markerClusterGroup(clusterOptions);
989+
//clusterGroup = L.markerClusterGroup(clusterOptions);
990+
clusterGroup = _leaflet2.default.markerClusterGroup.layerSupport(clusterOptions);
991+
if (clusterOptions.freezeAtZoom) {
992+
var freezeAtZoom = clusterOptions.freezeAtZoom;
993+
delete clusterOptions.freezeAtZoom;
994+
clusterGroup.freezeAtZoom(freezeAtZoom);
995+
}
990996
clusterGroup.clusterLayerStore = new _clusterLayerStore2.default(clusterGroup);
991997
}
992998
var extraInfo = cluster ? { clusterId: clusterId } : {};
@@ -1846,7 +1852,6 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
18461852
// pixel of the original image has some contribution to the downscaled image)
18471853
// as opposed to a single-step downscaling which will discard a lot of data
18481854
// (and with sparse images at small scales can give very surprising results).
1849-
18501855
var Mipmapper = function () {
18511856
function Mipmapper(img) {
18521857
_classCallCheck(this, Mipmapper);

inst/htmlwidgets/lib/Leaflet.label/leaflet.label-src.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,15 @@ L.FeatureGroup.include({
606606
},
607607

608608
unbindLabel: function () {
609-
return this.invoke('unbindLabel');
609+
//return this.invoke('unbindLabel');
610+
return this.invoke('hideLabel');
610611
},
611612

612613
updateLabelContent: function (content) {
613614
this.invoke('updateLabelContent', content);
614615
}
615616
});
616617

618+
617619
return LeafletLabel;
618620
}, window));

inst/htmlwidgets/lib/Leaflet.label/leaflet.label.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/plugins/Leaflet.markercluster/MarkerCluster.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@
33
-moz-transition: -moz-transform 0.3s ease-out, opacity 0.3s ease-in;
44
-o-transition: -o-transform 0.3s ease-out, opacity 0.3s ease-in;
55
transition: transform 0.3s ease-out, opacity 0.3s ease-in;
6-
}
6+
}
7+
8+
.leaflet-cluster-spider-leg {
9+
/* stroke-dashoffset (duration and function) should match with leaflet-marker-icon transform in order to track it exactly */
10+
-webkit-transition: -webkit-stroke-dashoffset 0.3s ease-out, -webkit-stroke-opacity 0.3s ease-in;
11+
-moz-transition: -moz-stroke-dashoffset 0.3s ease-out, -moz-stroke-opacity 0.3s ease-in;
12+
-o-transition: -o-stroke-dashoffset 0.3s ease-out, -o-stroke-opacity 0.3s ease-in;
13+
transition: stroke-dashoffset 0.3s ease-out, stroke-opacity 0.3s ease-in;
14+
}

0 commit comments

Comments
 (0)