Skip to content

Instantly share code, notes, and snippets.

@tim-salabim
Last active February 7, 2021 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tim-salabim/f1937720ed25903f583a91d83fcdbc33 to your computer and use it in GitHub Desktop.
Save tim-salabim/f1937720ed25903f583a91d83fcdbc33 to your computer and use it in GitHub Desktop.
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
@tim-salabim
Copy link
Author

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)

Peek 2021-02-07 17-51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment