Related threads
Closing info windows in google maps by clicking the map?
Google maps api close infowindow when clicking somewhere else
Google Maps: Auto close open InfoWindows?
Problem
I am able to target all markers with the close method and iterate over them with a for loop to manually close their respective infowindows using:
for (var i = 0; i < mapgoogleMarkersdefaultLayerId.length; i++) {
mapgoogleMarkersdefaultLayerId[i].infowindow.close();
}
However, I am planning to have all infowindows close once the user clicks anywhere else on the map. I attempted to use an addEventListener on the map object by:
map.addEventListener("click", function(event) {
for (var i = 0; i < mapgoogleMarkersdefaultLayerId.length; i++) {
mapgoogleMarkersdefaultLayerId[i].infowindow.close();
}
});
However, using the listener on the map object also captures the marker, thus immediately closing the infowindow once the user clicks the marker, resulting in nothing happening. I've tried to target various "base layers" of the google map using things such as referencing mapmap.__gm.panes.mapPane
to target the mappanes below the marker with no results.
More info
I am including this script as another file in my shiny app folder and linking it to my app via includeScript("closeInfoWindows.js") in the ui.
MCVE
library(shiny)
library(googleway)
ui <- fluidPage(
google_mapOutput(outputId = "map", height = "800px")
)
server <- function(input, output) {
tram_stops$info <- "Make me dissappear!"
set_key(api_key)
output$map <- renderGoogle_map({
google_map(data = tram_stops) %>%
add_markers(lat = "stop_lat", lon = "stop_lon", info_window = "info")
})
}
shinyApp(ui, server)