qwen3-235b-a22b· Alibaba (Qwen) LMArena Human Preference 140K
On a MapLibre map object, how can I search for certain features on a layer? And will it…
- User
On a MapLibre map object, how can I search for certain features on a layer? And will it give all features?
- Assistant
To search for specific features on a MapLibre layer, you can use two primary methods:
queryRenderedFeaturesandquerySourceFeatures. Here's how they work and their limitations:
1.
queryRenderedFeatures- Purpose: Retrieves features currently rendered on the map (i.e., visible in the viewport).
- Use Case: Useful for identifying features under a cursor, within a bounding box, or visible at the current zoom level.
- Syntax:
const features = map.queryRenderedFeatures( [pointOrBox], // e.g., [[x1, y1], [x2, y2]] for a box or [x, y] for a point { layers: ['layer-id'] } // optional: filter by layer(s) ); - Limitations:
- Only returns visible features (filtered by the current map viewport and zoom level).
- Respects layer-level filters and styling rules (e.g., if a layer hides features via
filter, they won't be included). - For vector tile sources, only returns features in the currently loaded tiles.
2.
querySourceFeatures- Purpose: Queries the raw source data (not limited to what’s rendered).
- Use Case: Useful for accessing all features in a source (if the data is fully loaded).
- Syntax:
const features = map.querySourceFeatures( 'source-id', { sourceLayer: 'vector-source-layer', // required for vector tiles filter: ['==', 'property', 'value'] // optional filter } ); - Limitations:
- For vector tile sources, only returns features in already loaded tiles (not the full dataset unless you’ve panned/zoomed to load all tiles).
- For GeoJSON sources, returns all features if the data is fully loaded (synchronous for local GeoJSON, but paginated for remote URLs).
- Does not respect layer-level filters (returns raw source data).
Will It Return All Features?
- Not guaranteed, especially for large datasets:
queryRenderedFeatures: Returns only visible features.querySourceFeatures: Returns all features only if the source is fully loaded (e.g., a small GeoJSON file). For tiled sources (vector tiles), it returns features from tiles already requested by the map.
How to Search for Specific Features
To find features matching certain criteria (e.g., a property value):
const allFeatures = map.queryRenderedFeatures({ layers: ['my-layer'] }); const filtered = allFeatures.filter(f => f.properties.name === 'Target');For source data:
const allSourceFeatures = map.querySourceFeatures('my-source-id', { sourceLayer: 'my-source-layer' }); const filtered = allSourceFeatures.filter(f => f.properties.status === 'active');
Best Practices
- For small datasets (e.g., GeoJSON):
- Use
querySourceFeatureswithout filters to get all features.
- Use
- For large datasets (e.g., vector tiles):
- Use
querySourceFeatureswith a filter to avoid memory issues. - Load tiles progressively as the user pans/zooms.
- Use
- Combine with client-side filtering to refine results further.
Example: Get All Features in a GeoJSON Source
const sourceFeatures = map.querySourceFeatures('geojson-source-id'); console.log(`Total features: ${sourceFeatures.length}`);Example: Find Features Under the Cursor
map.on('click', (e) => { const features = map.queryRenderedFeatures(e.point, { layers: ['my-layer'] }); console.log('Clicked features:', features); });By choosing the right method based on your use case and understanding its scope, you can efficiently search for features in MapLibre.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.