if (FALSE) {
# Pattern 1: Query first, then filter (RECOMMENDED)
proxy <- maplibre_proxy("map")
query_rendered_features(proxy, layer_id = "counties", callback = function(features) {
if (nrow(features) > 0) {
# Filter map based on query results - no race condition
proxy |> set_filter("selected", list("in", "id", features$id))
}
})
# Pattern 2: Use filter parameter to avoid race conditions
query_rendered_features(proxy,
filter = list(">=", "population", 50000),
callback = function(features) {
# These results are guaranteed to match the filter
print(paste("Found", nrow(features), "high population areas"))
})
# Query specific bounding box with callback
query_rendered_features(proxy, geometry = c(100, 100, 200, 200),
layer_id = "counties", callback = function(features) {
print(paste("Found", nrow(features), "features"))
})
# ANTI-PATTERN - Don't do this!
# proxy |> set_filter("layer", new_filter)
# query_rendered_features(proxy, layer_id = "layer") # Will get stale results!
}
Run the code above in your browser using DataLab