firmo23
firmo23

Reputation: 8454

Add a pop-up label in a leaflet heatmap

Is it possible to add a pop-label when the user mouse over a certain point in a leaflet heatmap? For example to see depth and stations from the quakes dataset.

library(leaflet)

leaflet(quakes) %>%
  addProviderTiles(providers$CartoDB.DarkMatter) %>%
  setView( 178, -20, 5 ) %>%
  addHeatmap(
    lng = ~long, lat = ~lat, intensity = ~mag,
    blur = 20, max = 0.05, radius = 15
  )

## for more examples see
# browseURL(system.file("examples/heatmaps.R", package = "leaflet.extras"))
kml <- readr::read_file(
  system.file("examples/data/kml/crimes.kml.zip", package = "leaflet.extras")
)

leaflet() %>%
  setView(-77.0369, 38.9072, 12) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addKMLHeatmap(kml, radius = 7) %>%
  addKML(
    kml,
    markerType = "circleMarker",
    stroke = FALSE, fillColor = "black", fillOpacity = 1,
    markerOptions = markerOptions(radius = 1))

Upvotes: 0

Views: 901

Answers (1)

user63230
user63230

Reputation: 4698

I'm not sure this is what you want but you can add marker popups in the usual way:

library(leaflet)
leaflet(quakes) %>%
  addProviderTiles(providers$CartoDB.DarkMatter) %>%
  setView( 178, -20, 5 ) %>%
  addHeatmap(
    lng = ~long, lat = ~lat, intensity = ~mag,
    blur = 20, max = 0.05, radius = 15
  ) %>% 
  addMarkers(lng = quakes$long, lat = quakes$lat, 
             popup = paste("Depth", quakes$depth, "<br>",
                           "Stations:", quakes$stations))

enter image description here

if you dont want the dominating markers visible you could add circle markers but set the fillOpacity to zero:

leaflet(quakes) %>%
  addProviderTiles(providers$CartoDB.DarkMatter) %>%
  setView( 178, -20, 5 ) %>%
  addHeatmap(
    lng = ~long, lat = ~lat, intensity = ~mag,
    blur = 20, max = 0.05, radius = 15
  ) %>% 
  addCircleMarkers(lng = quakes$long, lat = quakes$lat, 
                   fillOpacity = 0, weight = 0,
                   popup = paste("Depth:", quakes$depth, "<br>",
                                 "Stations:", quakes$stations),
                   labelOptions = labelOptions(noHide = TRUE))

enter image description here

Upvotes: 1

Related Questions