4

I have a fairly simple leaflet project in RStudio

library(rgdal)
library(leaflet)
library(htmlwidgets)

#load data/layers
m = leaflet() %>% {add polygons....}
saveWidget(m, file="HTML_File.html", selfcontained = F)

The resulting HTML page displays nicely, but I would want to respond to a click event by adding a pop-up that provides the lat / lon of the point clicked, as well as the attribute of the top-most layer.

I have no idea how to go about it. I noticed that the leaflet.js script that gets exported has code to handle double clicks. I tried to hack that code to provide an alert, along the lines of

alert("Lat, Lon : " + e.latlng.lat + ", " + e.latlng.lng)   

but clearly I don't know what I'm doing.

  • 1
    I agree pajonk's in that Shiny is the best solution for your issue, but if you're happy (to start with) with displaying coordinates on hover, the mapview package offers a function for this called addMouseCoordinates. – Jul Jan 15 '18 at 4:30
2

Looks like you need more powerful tool to handle your problem. The solution in R may be using Shiny - basics in this link: https://rstudio.github.io/leaflet/shiny.html

input$MAPID_click is an event that is sent when the map background or basemap is clicked. The value is a list with lat and lng.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.