-
Notifications
You must be signed in to change notification settings - Fork 504
Expand file tree
/
Copy pathmethods.R
More file actions
115 lines (102 loc) · 3.19 KB
/
methods.R
File metadata and controls
115 lines (102 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#' Methods to manipulate the map widget
#'
#' A series of methods to manipulate the map.
#' @param map a map widget object created from [leaflet()]
#' @param lng The longitude of the map center
#' @param lat The latitude of the map center
#' @param zoom the zoom level
#' @param options a list of zoom/pan options (see
#' <https://web.archive.org/web/20220702182250/https://leafletjs.com/reference-1.3.4.html#zoom/pan-options>)
#' @references <https://web.archive.org/web/20220702182250/https://leafletjs.com/reference-1.3.4.html#map-methods-for-modifying-map-state>
#' @return The modified map widget.
#' @describeIn map-methods Set the view of the map (center and zoom level)
#' @export
#' @examples
#' \donttest{m <- leaflet() %>% addTiles() %>% setView(-71.0382679, 42.3489054, zoom = 18)
#' m # the RStudio 'headquarter'
#' m %>% fitBounds(-72, 40, -70, 43)
#' m %>% clearBounds() # world view
#' }
setView <- function(map, lng, lat, zoom, options = list()) {
view <- evalFormula(list(c(lat, lng), zoom, options))
dispatch(map,
"setView",
leaflet = {
changeView(map, setView = view)
},
leaflet_proxy = {
invokeRemote(map, "setView", view)
map
}
)
}
#' @describeIn map-methods Flys to a given location/zoom-level using smooth pan-zoom.
#' @export
flyTo <- function(map, lng, lat, zoom, options = list()) {
view <- evalFormula(list(c(lat, lng), zoom, options))
dispatch(map,
"flyTo",
leaflet = {
changeView(map, flyTo = view)
},
leaflet_proxy = {
invokeRemote(map, "flyTo", view)
map
}
)
}
#' @describeIn map-methods Set the bounds of a map
#' @param lng1,lat1,lng2,lat2 the coordinates of the map bounds
#' @export
fitBounds <- function(map, lng1, lat1, lng2, lat2, options = list()) {
bounds <- evalFormula(list(lat1, lng1, lat2, lng2, options), getMapData(map))
dispatch(map,
"fitBounds",
leaflet = {
changeView(map, fitBounds = bounds)
},
leaflet_proxy = {
invokeRemote(map, "fitBounds", bounds)
map
}
)
}
#' @describeIn map-methods Flys to given bound using smooth pan/zoom.
#' @export
flyToBounds <- function(map, lng1, lat1, lng2, lat2, options = list()) {
bounds <- evalFormula(list(lat1, lng1, lat2, lng2, options), getMapData(map))
dispatch(map,
"flyToBounds",
leaflet = {
changeView(map, flyToBounds = bounds)
},
leaflet_proxy = {
invokeRemote(map, "flyToBounds", bounds)
map
}
)
}
#' @describeIn map-methods Restricts the map view to the given bounds
#' @export
setMaxBounds <- function(map, lng1, lat1, lng2, lat2) {
invokeMethod(map, getMapData(map), "setMaxBounds", lat1, lng1, lat2, lng2)
}
#' @describeIn map-methods Clear the bounds of a map, and the bounds will be
#' automatically determined from latitudes and longitudes of the map elements
#' if available (otherwise the full world view is used)
#' @export
clearBounds <- function(map) {
dispatch(map,
"clearBounds",
leaflet = {
changeView(map)
}
)
}
changeView <- function(map, setView = NULL, fitBounds = NULL, flyTo = NULL, flyToBounds = NULL) {
map$x$setView <- setView
map$x$fitBounds <- fitBounds
map$x$flyTo <- flyTo
map$x$flyToBounds <- flyToBounds
map
}