Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to transition from Heatmap to circle markers on zooming? #193

Closed
chintanp opened this issue Apr 15, 2020 · 1 comment
Closed

How to transition from Heatmap to circle markers on zooming? #193

chintanp opened this issue Apr 15, 2020 · 1 comment
Labels

Comments

@chintanp
Copy link

I am wondering if it would be possible to recreate the mapbox-like heatmap in R+Leaflet(.x). The idea is that the upon zooming the heatmap layer transitions to circle markers.

@trafficonese
Copy link
Owner

Unfortunately, the example link doesnt exist anymore.

Anyway, so far there is no automatic way of doing this.
But you can certainly do it manually and show the heatmap with map-zoom levels < 7 and the circle markers with zoom levels > 7.

Here is an example in Shiny:

library(shiny)  
library(leaflet)

ui <- fluidPage(
  verbatimTextOutput("zoomlevel"),
  leafletOutput("map", height = 800)
)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet()  %>% 
      addTiles() %>% 
      setView(178, -20, 5)
  })
  
  output$zoomlevel <- renderPrint({
    paste0("Zoom level: ", req(input$map_zoom))
  })
  observe({
    zoom <- req(input$map_zoom)
    if (zoom < 7) {
      leafletProxy("map") %>%
        hideGroup("markers") %>% 
        showGroup("heatmap") %>% 
        addHeatmap(data = quakes,
            lng = ~long, lat = ~lat, intensity = ~mag,
            blur = 20, max = 0.05, radius = 15, 
            group = "heatmap")
    } else {
      pal <- colorNumeric("inferno", quakes$depth)
      leafletProxy("map") %>%
        hideGroup("heatmap") %>% 
        showGroup("markers") %>% 
        leaflet::addCircleMarkers(
          data = quakes, color = ~pal(depth),
          lng = ~long, lat = ~lat, radius = 4,
          group = "markers")
    }
  })
}
shinyApp(ui, server)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants