Skip to content

Commit f392477

Browse files
committed
More complex examples and Fixed #11
1 parent b6c980d commit f392477

File tree

4 files changed

+163
-8
lines changed

4 files changed

+163
-8
lines changed

inst/examples/awesomeMarkers.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,4 @@ leaflet(cities) %>% addTiles() %>%
7878
addAwesomeMarkers(lng = ~Long, lat = ~Lat,
7979
label = ~City,
8080
labelOptions = rep(labelOptions(noHide = T),nrow(cities)),
81-
icon = ~popIcons[PopCat]
82-
)
81+
icon = ~popIcons[PopCat] )

inst/examples/geojson.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
library(leaflet)
3+
#' <br/><br/>
4+
#' The V8 part is simply to read the JSON embeded in the Javascript.<br/>
5+
#' For a geojson file `jsonlite::fromfromJSON()` or `geojsonio::regeojson_read()` will do
6+
#'
7+
jsURL <- 'https://rawgit.com/Norkart/Leaflet-MiniMap/master/example/local_pubs_restaurant_norway.js'
8+
v8 <- V8::v8()
9+
v8$source(jsURL)
10+
geoJson <- geojsonio::as.json(v8$get('pubsGeoJSON'))
11+
12+
# This is the kicker, convert geojson to a Spatial object.
13+
# This then allows us to use formulas in our markers, polygons etc.
14+
spdf <- geojsonio::geojson_sp(geoJson)
15+
16+
icons <- awesomeIconList(
17+
pub = makeAwesomeIcon(icon='glass', library='fa', markerColor = 'red'),
18+
restaurant = makeAwesomeIcon(icon='cutlery', library='fa', markerColor = 'blue')
19+
)
20+
21+
leaflet() %>% addTiles() %>%
22+
setView(10.758276373601069, 59.92448055859924, 13) %>%
23+
addAwesomeMarkers(data=spdf,
24+
label=~stringr::str_c(amenity,': ', name),
25+
icon = ~icons[amenity],
26+
options = markerOptions(riseOnHover = TRUE, opacity = 0.75),
27+
group = 'pubs')

inst/examples/minimap.R

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,104 @@ m %>%
3232
addMiniMap(
3333
tiles = esri[[1]],
3434
toggleDisplay = T) %>%
35-
htmlwidgets::onRender("function(el, x) { var myMap = this; this.on('baselayerchange', function (e) { myMap.minimap.changeLayer(L.tileLayer.provider(e.name)); }) }")
35+
htmlwidgets::onRender("function(el, x) {
36+
var myMap = this;
37+
this.on('baselayerchange',
38+
function (e) {
39+
myMap.minimap.changeLayer(L.tileLayer.provider(e.name));
40+
})
41+
}")
3642

43+
#' <br/><br/>
44+
#' Another advanced use case
45+
#' Minimap with markers.
46+
#' Note the use of 'group' ID to find markers in main map and add corresponding CircleMarkers in minimap.
47+
#' The V8 part is simply to read the JSON embeded in the Javascript.<br/>
48+
#' For a geojson file `jsonlite::fromfromJSON()` or `geojsonio::regeojson_read()` will do
49+
#'
50+
jsURL <- 'https://rawgit.com/Norkart/Leaflet-MiniMap/master/example/local_pubs_restaurant_norway.js'
51+
v8 <- V8::v8()
52+
v8$source(jsURL)
53+
geoJson <- geojsonio::as.json(v8$get('pubsGeoJSON'))
54+
55+
# This is the kicker, convert geojson to a Spatial object.
56+
# This then allows us to use formulas in our markers, polygons etc.
57+
spdf <- geojsonio::geojson_sp(geoJson)
58+
59+
icons <- awesomeIconList(
60+
pub = makeAwesomeIcon(icon='glass', library='fa', markerColor = 'red'),
61+
restaurant = makeAwesomeIcon(icon='cutlery', library='fa', markerColor = 'blue')
62+
)
63+
64+
leaflet() %>% addTiles() %>%
65+
setView(10.758276373601069, 59.92448055859924, 13) %>%
66+
addAwesomeMarkers(data=spdf,
67+
label=~stringr::str_c(amenity,': ', name),
68+
icon = ~icons[amenity],
69+
options = markerOptions(riseOnHover = TRUE, opacity = 0.75),
70+
group = 'pubs') %>%
71+
addMiniMap() %>%
72+
htmlwidgets::onRender("function(el, t) {
73+
var myMap = this;
74+
75+
var pubs = myMap.layerManager._byGroup.pubs;
76+
var pubs2 = new L.FeatureGroup();
77+
78+
for(pub in pubs) {
79+
var m = new L.CircleMarker(pubs[pub]._latlng,
80+
{radius: 2});
81+
pubs2.addLayer(m);
82+
83+
}
84+
var layers = new L.LayerGroup([myMap.minimap._layer, pubs2]);
85+
86+
myMap.minimap.changeLayer(layers);
87+
}")
88+
89+
#' <br/><br/>
90+
#' Finally combine the approaches in last 2 examples
91+
#' Minimap w/ changable layers and circle markers.
92+
m <- leaflet()
93+
esri <- providers %>%
94+
purrr::keep(~ grepl('^Esri',.))
95+
96+
esri %>%
97+
purrr::walk(function(x) m <<- m %>% addProviderTiles(x,group=x))
98+
99+
m %>%
100+
setView(10.758276373601069, 59.92448055859924, 13) %>%
101+
addAwesomeMarkers(data=spdf,
102+
label=~stringr::str_c(amenity,': ', name),
103+
icon = ~icons[amenity],
104+
options = markerOptions(riseOnHover = TRUE, opacity = 0.75),
105+
group = 'pubs') %>%
106+
addLayersControl(
107+
baseGroups = names(esri),
108+
options = layersControlOptions(collapsed = FALSE)
109+
) %>%
110+
addMiniMap(tiles = esri[[1]],
111+
toggleDisplay = T) %>%
112+
htmlwidgets::onRender("function(el, t) {
113+
var myMap = this;
114+
115+
var pubs = myMap.layerManager._byGroup.pubs;
116+
var pubs2 = new L.FeatureGroup();
117+
118+
for(pub in pubs) {
119+
var m = new L.CircleMarker(pubs[pub]._latlng,
120+
{radius: 2});
121+
pubs2.addLayer(m);
122+
123+
}
124+
var layers = new L.LayerGroup([myMap.minimap._layer, pubs2]);
125+
126+
myMap.minimap.changeLayer(layers);
127+
128+
myMap.on('baselayerchange',
129+
function (e) {
130+
debugger;
131+
myMap.minimap.changeLayer(
132+
new L.LayerGroup([L.tileLayer.provider(e.name),
133+
pubs2]));
134+
});
135+
}")

inst/examples/providers.R

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
# Now that there is a providers list
2-
# You can programmatically add providers
3-
# Here I show how to add all 'ESRI' provided basemaps.
4-
# Checkout the providers list for all available providers.
5-
1+
#' Now that there is a providers list
2+
#' You can programmatically add providers.<br/>
3+
#' Here I show how to add all 'ESRI' provided basemaps.
4+
#' Checkout the `providers` list for all available providers.<br/>
5+
#' <br/>
66
library(leaflet)
77

88
m <- leaflet() %>% setView(0,0,1)
99

10+
# Take out ESRI provided tiles
1011
esri <- providers %>%
1112
purrr::keep(~ grepl('^Esri',.))
1213

@@ -16,5 +17,34 @@ esri %>%
1617
m %>%
1718
addLayersControl(
1819
baseGroups = names(esri),
20+
options = layersControlOptions(collapsed = TRUE)
21+
)
22+
23+
#' <br/><br/><br/><br/>
24+
#' providers with options
25+
#' Change the accessToken with your mapbox token in options below
26+
#' The one here may not work always
27+
mapbox.tileIds <- list(Satellite='mapbox.satellite',
28+
Terrian='mapbox.mapbox-terrain-v2',
29+
Comic='bhaskarvk.1cm89o4e',
30+
'High Contrast'='bhaskarvk.1biainl5')
31+
32+
m <- leaflet() %>% setView(0,0,1)
33+
34+
names(mapbox.tileIds) %>%
35+
purrr::walk(function(x) {
36+
m <<- m %>%
37+
addProviderTiles(providers$MapBox, group = x,
38+
options = providerTileOptions(
39+
detectRetina = TRUE,
40+
# id and accessToken are Mapbox specific options
41+
id = mapbox.tileIds[[x]] ,
42+
accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN')
43+
))
44+
})
45+
46+
m %>%
47+
addLayersControl(
48+
baseGroups = names(mapbox.tileIds),
1949
options = layersControlOptions(collapsed = FALSE)
2050
)

0 commit comments

Comments
 (0)