Last active
February 7, 2021 16:54
-
-
Save tim-salabim/f1937720ed25903f583a91d83fcdbc33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(leaflet) | |
map = leaflet() %>% | |
addTiles() | |
m = htmlwidgets::onRender( | |
map, | |
jsCode = htmlwidgets::JS( | |
"function(el,x,data){ | |
var map = this; | |
var tst = L.circleMarker([30, 30]); | |
L.polyline([[0, -180],[0, 180]], { | |
//'dashArray': [10, 50] | |
}) | |
.on('click', function (e) { | |
if (!map.hasLayer(tst)) { | |
map.addLayer(tst); | |
} | |
console.log(map.hasLayer(tst)); | |
e.target.setStyle({ | |
color: 'red' | |
}); | |
}) | |
.on('contextmenu', function (e) { | |
e.target.setStyle({ | |
color: 'blue' | |
}); | |
if (map.hasLayer(tst)) { | |
map.removeLayer(tst); | |
} | |
console.log(map.hasLayer(tst)); | |
}) | |
.addTo(map); | |
} | |
" | |
) | |
) | |
m |
Author
tim-salabim
commented
Jan 31, 2021
This is now available through leafem:::addReactiveFeatures()
library(leaflet)
library(leafem)
library(sf)
library(geojsonsf)
fran = mapview::franconia
brew = st_join(mapview::breweries, fran)
leaflet() %>%
addTiles() %>%
addGeoJSON(sf_geojson(fran), group = "fran") %>%
leafem:::addReactiveFeatures(
brew
, bindTo = "fran"
, by = "NUTS_ID"
, on = "click"
, group = "brew"
, style = list(color = "black", fillOpacity = 0.9)
) %>%
addMouseCoordinates() %>%
setView(lng = 10.75, lat = 49.75, zoom = 8)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment