0

I have this code for an interactive map in R:

library(leaflet)
library(inlmisc)

Long = rnorm(1000, -71, 0.5)
Lat = rnorm(1000, 42.3, 0.5)

loc = rep("loc", 1000)
Name = rep("Location", 1000)
num = 1:1000
Label = paste0(loc, "_", num)
Location = paste0(Name, "_", num)

df = data.frame(Name, Lat, Long, Label)

map <- leaflet(df) %>%   addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions(), popup = ~paste("title: ", Name)) %>%
    addTiles() %>%
    setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
    addMarkers(~Long, ~Lat, popup = ~Name, group="marker", label = ~Label) %>% 
    inlmisc::AddSearchButton(group = "marker", zoom = 15,
                             textPlaceholder = "Search here")

The map seems to work fine - but the "icons" are being displayed twice (i.e. blue pins and colored circles, e.g. yellow, green):

enter image description here

Is there a way to have it such that when you zoom out, the blue pins collapse into the colorful circles - and when you zoom in, the colorful circles collapse into the blue pins?

Thank you!

2 Answers 2

1

It looks like you are adding the markers twice.

The first addMarkers line, does the clustering with zoom that it sound like you are interested in.

You can add the group and label options to the first addMarkers call, to be able to search and have mouse-over labels as well as the clustering.

map <- leaflet(df) %>% addProviderTiles(providers$OpenStreetMap) %>%
  addMarkers( clusterOptions = markerClusterOptions(), 
              popup = ~paste("title: ", Name),
              group="marker", label = ~Label) %>%
  addTiles() %>%
  setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
  inlmisc::AddSearchButton(group = "marker", zoom = 15,
                           textPlaceholder = "Search here")
1
  • @ Senithil913 : You perfectly solved this question - thank you so much!
    – stats_noob
    Commented Oct 10, 2022 at 17:23
0

Using the answer of @Senithil913 - I added a reset option:

map <- leaflet(df) %>% addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions(), 
                popup = ~paste("title: ", Name),
                group="marker", label = ~Label) %>%
    addTiles() %>%
    setView(lng=-71.0589,lat=42.3301, zoom=12) %>%
    inlmisc::AddSearchButton(group = "marker", zoom = 15,
                             textPlaceholder = "Search here") %>%  addResetMapButton() 

Not the answer you're looking for? Browse other questions tagged or ask your own question.