SymbolixAU / googleway

R Package for accessing and plotting Google Maps
http://symbolixau.github.io/googleway/
Other
234 stars 46 forks source link

Update google map location center in Shiny app #261

Closed ssnyder20 closed 1 year ago

ssnyder20 commented 2 years ago

Hello!

I am trying to update the center location of a google_map() based on a reactive input. Similar to the question asked here. I am doing this in an observeEvent with google_map_update by updating the markers. However, it updates the marker to the correct location, but the center location stays the same, and it zooms out so the map contains the location. I'd rather have the zoom stay fixed and the center updated to the lat/lon of the reactive input.

There also seems to be an easier solution using google_map_view, from the thread here. I'm using googleway 2.7.6 and it doesn't recognize that function. Has this been removed or renamed? Cause this seems to be a solution if it's still available.

Below is some code in my server.R:

  output$map <- renderGoogle_map({

    data <- csv %>% filter(name == rsrt())
    google_map(key = gkey, map_type = 'hybrid', data = data) %>%
      add_markers(lat = "lat", lon = "lon")

  })
  observeEvent(rsrt(),{

    data <- csv %>% filter(name == rsrt())
    google_map_update("map", data = data) %>%
      clear_markers()

    google_map_update("map", data = data) %>%
      add_markers(lat = "lat", lon  = "lon")

  })

Let me know if you have any insight. I appreciate the help!

tnelsen commented 1 year ago

To use the update_map_view option within the add_markers function you have to have the dev version of the package (2.7.6001). It does seem like that option would fix your problem as well! Those changes were added after the most recent CRAN release (2.7.6).

ssnyder20 commented 1 year ago

To use the update_map_view option within the add_markers function you have to have the dev version of the package (2.7.6001). It does seem like that option would fix your problem as well! Those changes were added after the most recent CRAN release (2.7.6).

Awesome, that worked. Thank you!