Customizing Leaflet Search Options

I have this following map made from simulated data in R:

First I loaded the libraires:


Then, I simulated the random data for this example:

myFun <- function(n = 5000) {
  a <-, replicate(5, sample(LETTERS, n, TRUE), FALSE))
  paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))

Lat = abs(rnorm(1000, 42.2, 0.1))
Long = abs(rnorm(1000, -70, 0.1))
City = myFun(1000)

cities = data.frame(Lat, Long, City)

Finally, I made the map:

# download icon from here:

leaflet(cities) %>%
    addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions(), popup = ~paste("title: ", City)) %>%
                    addResetMapButton() %>%
                    # these markers will be "invisible" on the map:
                        data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
                        group = 'cities', # this is the group to use in addSearchFeatures()
                        # make custom icon that is so small you can't see it:
                        icon = makeIcon(
                            iconUrl = "leaf-green.png",
                            iconWidth = 1, iconHeight = 1
                    ) %>%
                        targetGroups = 'cities', # group should match addMarkers() group
                        options = searchFeaturesOptions(
                            zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
                            autoCollapse = TRUE, hideMarkerOnCollapse = TRUE

If I try using the search feature in this map and look for an individual point (e.g. "QNZLF4655A" ):

enter image description here

All the other points are displayed at the same time - is there a way to prevent this and only show the point (i.e. the blue pin) that is being searched for?

Thank you!

NOTE: Sometimes if the map is too much "zoomed out" - when I search for a city in the search bar, a red circle briefly appears and the searched city does not appear at all:

enter image description here

I was hoping that when a city is searched, the map will automatically show that city with it's "identification label tag". Is there a way to fix this?

This topic was automatically closed 42 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.