diff --git a/src/source/geojson_source.ts b/src/source/geojson_source.ts index a7aa4c1bd85..b77069be00d 100644 --- a/src/source/geojson_source.ts +++ b/src/source/geojson_source.ts @@ -11,7 +11,7 @@ import type Tile from './tile'; import type Actor from '../util/actor'; import type {Callback} from '../types/callback'; import type {GeoJSONWorkerOptions} from './geojson_worker_source'; -import type {GeoJSONSourceSpecification, PromoteIdSpecification} from '../style-spec/types'; +import type {GeoJSONSourceSpecification, PromoteIdSpecification, FilterSpecification} from '../style-spec/types'; import type {Cancelable} from '../types/cancelable'; /** @@ -253,6 +253,48 @@ class GeoJSONSource extends Evented implements ISource { return this; } + /** + * Sets filter for the GeoJSON data source and re-renders the map. + * + * @param {FilterSpecification} filter A FilterSpecification type for the filter expression. + * @returns {GeoJSONSource} Returns itself to allow for method chaining. + * @example + * map.addSource('source_id', { + * type: 'geojson', + * data: { + * "type": "FeatureCollection", + * "features": [{ + * "type": "Feature", + * "properties": {"name": "Null Island"}, + * "geometry": { + * "type": "Point", + * "coordinates": [ 0, 0 ] + * } + * }, + * { + * "type": "Feature", + * "properties": {"name": "Another Island"}, + * "geometry": { + * "type": "Point", + * "coordinates": [ 1, 1 ] + * } + * }] + * } + * }); + * const geojsonSource = map.getSource('source_id'); + * // Update the filter after the GeoJSON source was created + * geojsonSource.setFilter([ + * "==", + * ["get", "name"], + * "Another Island" + * ]); + */ + setFilter(filter?: FilterSpecification): this { + this.workerOptions = extend({filter}, this.workerOptions); + this._updateWorkerData(); + return this; + } + /** * For clustered sources, fetches the zoom at which the given cluster expands. *