1

Using googleway R package with Shiny and updating the map with google_map_update and update_heatmap.

Is it possible to maintain the same zoom level and position during calls to google_map_update - it currently zooms out and pans around automatically when points change coordinates which is a headache - can this be turned off?

Code below is from the link, with automation turned on for the slider. Zoom in to the map and then animate and you'll see the auto zoom and panning occur.

library(shiny)
library(googleway)

ui <- fluidPage(
  sliderInput(inputId = "sample", label = "sample", min = 1, max = 10,
              step = 1, value = 10, animate = TRUE),
  google_mapOutput(outputId = "map")
)

server <- function(input, output){

  map_key <- ''

  set.seed(20170417)
  df <- tram_route[sample(1:nrow(tram_route), size = 10 * 100, replace = T), ]

  output$map <- renderGoogle_map({
    google_map(key = map_key) %>%
      add_heatmap(data = df, lat = "shape_pt_lat", lon = "shape_pt_lon",
                  option_radius = 0.001)
  })

  observeEvent(input$sample,{

    df <- tram_route[sample(1:nrow(tram_route), size = input$sample * 100, replace = T), ]

    google_map_update(map_id = "map") %>%
      update_heatmap(data = df, lat = "shape_pt_lat", lon = "shape_pt_lon")
  })
}

shinyApp(ui, server)
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
Vlad
  • 3,058
  • 4
  • 25
  • 53

1 Answers1

2

Of course!

use update_map_view = FALSE


Docs

?update_heatmap

update_map_view - logical specifying if the map should re-centre according to the shapes


Example

google_map_update(map_id = "map") %>%
  update_heatmap(
    data = df
    , lat = "shape_pt_lat"
    , lon = "shape_pt_lon"
    , update_map_view = F
  )

In fact I would now recommend to always use update_map_view = FALSE as it is more memory efficient (for the browser) as you're not constantly updating an array of the map 'bounds'. But I haven't formally documented this anywhere yet.

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139