Combining Leaflet Maps Together?

I generated these 2 random dataset about geographical coordinates (e.g. each point represents an imaginary restaurant in France):

    id = 1:1000
    long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
    lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
    
    my_data_1 = data.frame(id, lat, long)

id = 1:1000
    long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
    lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
    

   my_data_2 = data.frame(id, lat, long)

I then made these 4 maps:

library(leaflet)
library(leaflet.extras)


map1 = my_data_1 %>%
    leaflet() %>%
    addTiles() %>%
    addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)


map2 = my_data_2 %>%
    leaflet() %>%
    addTiles() %>%
    addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)


map3 = my_data_1 %>% 
  leaflet() %>% 
  addTiles() %>% 
  addMarkers(clusterOption=markerClusterOptions())

map4 = my_data_2 %>% 
  leaflet() %>% 
  addTiles() %>% 
  addMarkers(clusterOption=markerClusterOptions())

I would like to combine all these 4 maps into a single map (i.e. a single html file). That is, I would like there to be a window with 4 toggle buttons :map1, map2, map3, map4. I would like that someone could click these 4 buttons and the corresponding map would load.

I found this link over here that shows how to combine maps (Leaflet for R - Show/Hide Layers) - but I do not think that this can be directly used to solve the problem I am working on. This would have been useful if I had different "classes" of restaurants (e.g. cafes, vegan restaurant, fancy restaurant, etc.) and I wanted to toggle between the different restaurants on the same map - however, I want to toggle between 4 completely different maps.

  • I am wondering: is there a straightforward option within the "leaflet" library that can be used to do this? Or does a function need to be written to accomplish this?

Thank you!

You could do this. Use the layer control in the bottom left to choose what to show.

library(leaflet)
library(leaflet.extras)

leaflet() %>%
  addTiles() %>%
  addHeatmap(data = my_data_1, lng=~long,lat=~lat,max=100,radius=20,blur=10, group = "a") %>% 
  addHeatmap(data = my_data_2, lng=~long,lat=~lat,max=100,radius=20,blur=10, group = "b") %>% 
  addMarkers(data = my_data_1, clusterOption=markerClusterOptions(), group = "c") %>% 
  addMarkers(data = my_data_2, clusterOption=markerClusterOptions(), group = "d") %>% 
  addLayersControl(overlayGroups = c("a", "b", "c", "d"),
                   position = "bottomleft") %>% 
  hideGroup(c("b", "c", "d")) # a is by default on

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.