Customizing Leaflet Search Options

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

First I loaded the libraires:

library(leaflet)
library(leaflet.extras)
library(dplyr)

Then, I simulated the random data for this example:

myFun <- function(n = 5000) {
  a <- do.call(paste0, 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: https://leafletjs.com/examples/custom-icons/leaf-green.png

leaflet(cities) %>%
    addProviderTiles(providers$OpenStreetMap) %>%
    addMarkers( clusterOptions = markerClusterOptions(), popup = ~paste("title: ", City)) %>%
                    addResetMapButton() %>%
                    # these markers will be "invisible" on the map:
                    addMarkers(
                        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
                        )
                    ) %>%
                    addSearchFeatures(
                        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.