Skip to content

Both copy text to your clipboard — Build with AI copies a setup prompt to paste into Claude Code, Cursor, Codex or Copilot; Copy page as Markdown copies this page to paste into a chat. How it works

GeoJSONSource ​

Defined in: src/source/geojson_source.ts:111

A source containing GeoJSON. (See the Style Specification for detailed documentation of options.)

Examples ​

ts
map.addSource('some id', {
    type: 'geojson',
    data: 'https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_10m_ports.geojson'
});
ts
map.addSource('some id', {
   type: 'geojson',
   data: {
       "type": "FeatureCollection",
       "features": [{
           "type": "Feature",
           "properties": {},
           "geometry": {
               "type": "Point",
               "coordinates": [
                   -76.53063297271729,
                   39.18174077994108
               ]
           }
       }]
   }
});
ts
map.getSource('some id').setData({
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "properties": { "name": "Null Island" },
      "geometry": {
          "type": "Point",
          "coordinates": [ 0, 0 ]
      }
  }]
});

See ​

Extends ​

Implements ​

Methods ​

_updateWorkerData() ​

_updateWorkerData(diff?: GeoJSONSourceDiff): Promise<void>

Defined in: src/source/geojson_source.ts:380

Responsible for invoking WorkerSource's geojson.loadData target, which handles loading the geojson data and preparing to serve it up as tiles, using geojson-vt or supercluster as appropriate.

Parameters ​

ParameterTypeDescription
diff?GeoJSONSourceDiffthe diff object

Returns ​

Promise<void>


abortTile() ​

abortTile(tile: Tile): Promise<void>

Defined in: src/source/geojson_source.ts:456

Allows to abort a tile loading.

Parameters ​

ParameterTypeDescription
tileTileThe tile to abort

Returns ​

Promise<void>

Implementation of ​

Source.abortTile


getBounds() ​

getBounds(): Promise<LngLatBounds>

Defined in: src/source/geojson_source.ts:274

Allows getting the source's boundaries. If there's a problem with the source's data, it will return an empty LngLatBounds.

Returns ​

Promise<LngLatBounds>

a promise which resolves to the source's boundaries


getClusterChildren() ​

getClusterChildren(clusterId: number): Promise<Feature<Geometry, {[name: string]: any; }>[]>

Defined in: src/source/geojson_source.ts:335

For clustered sources, fetches the children of the given cluster on the next zoom level (as an array of GeoJSON features).

Parameters ​

ParameterTypeDescription
clusterIdnumberThe value of the cluster's cluster_id property.

Returns ​

Promise<Feature<Geometry, {[name: string]: any; }>[]>

a promise that is resolved when the features are retrieved


getClusterExpansionZoom() ​

getClusterExpansionZoom(clusterId: number): Promise<number>

Defined in: src/source/geojson_source.ts:325

For clustered sources, fetches the zoom at which the given cluster expands.

Parameters ​

ParameterTypeDescription
clusterIdnumberThe value of the cluster's cluster_id property.

Returns ​

Promise<number>

a promise that is resolved with the zoom number


getClusterLeaves() ​

getClusterLeaves(clusterId: number, limit: number, offset: number): Promise<Feature<Geometry, {[name: string]: any; }>[]>

Defined in: src/source/geojson_source.ts:364

For clustered sources, fetches the original points that belong to the cluster (as an array of GeoJSON features).

Parameters ​

ParameterTypeDescription
clusterIdnumberThe value of the cluster's cluster_id property.
limitnumberThe maximum number of features to return.
offsetnumberThe number of features to skip (e.g. for pagination).

Returns ​

Promise<Feature<Geometry, {[name: string]: any; }>[]>

a promise that is resolved when the features are retrieved

Example ​

Retrieve cluster leaves on click

ts
map.on('click', 'clusters', (e) => {
  let features = map.queryRenderedFeatures(e.point, {
    layers: ['clusters']
  });

  let clusterId = features[0].properties.cluster_id;
  let pointCount = features[0].properties.point_count;
  let clusterSource = map.getSource('clusters');

  const features = await clusterSource.getClusterLeaves(clusterId, pointCount);
  // Print cluster leaves in the console
  console.log('Cluster leaves:', features);
});

getData() ​

getData(): Promise<GeoJSON<Geometry, {[name: string]: any; }>>

Defined in: src/source/geojson_source.ts:257

Allows to get the source's actual GeoJSON data.

Returns ​

Promise<GeoJSON<Geometry, {[name: string]: any; }>>

a promise which resolves to the source's actual GeoJSON data


hasTransition() ​

hasTransition(): boolean

Defined in: src/source/geojson_source.ts:481

True if the source has transition, false otherwise.

Returns ​

boolean

Implementation of ​

Source.hasTransition


listens() ​

listens(type: string): boolean

Defined in: src/util/evented.ts:165

Returns a true if this instance of Evented or any forwardeed instances of Evented have a listener for the specified type.

Parameters ​

ParameterTypeDescription
typestringThe event type

Returns ​

boolean

true if there is at least one registered listener for specified event type, false otherwise

Inherited from ​

Evented.listens


loaded() ​

loaded(): boolean

Defined in: src/source/geojson_source.ts:424

True if the source is loaded, false otherwise.

Returns ​

boolean

Implementation of ​

Source.loaded


loadTile() ​

loadTile(tile: Tile): Promise<void>

Defined in: src/source/geojson_source.ts:428

This method does the heavy lifting of loading a tile. In most cases it will defer the work to the relevant worker source.

Parameters ​

ParameterTypeDescription
tileTileThe tile to load

Returns ​

Promise<void>

Implementation of ​

Source.loadTile


off() ​

off(type: string, listener: Listener): GeoJSONSource

Defined in: src/util/evented.ts:90

Removes a previously registered event listener.

Parameters ​

ParameterTypeDescription
typestringThe event type to remove listeners for.
listenerListenerThe listener function to remove.

Returns ​

GeoJSONSource

Inherited from ​

Evented.off


on() ​

on(type: string, listener: Listener): Subscription

Defined in: src/util/evented.ts:73

Adds a listener to a specified event type.

Parameters ​

ParameterTypeDescription
typestringThe event type to add a listen for.
listenerListenerThe function to be called when the event is fired. The listener function is called with the data object passed to fire, extended with target and type properties.

Returns ​

Subscription

Inherited from ​

Evented.on


onAdd() ​

onAdd(map: Map): void

Defined in: src/source/geojson_source.ts:215

This method is called when the source is added to the map.

Parameters ​

ParameterTypeDescription
mapMapThe map instance

Returns ​

void

Implementation of ​

Source.onAdd


once() ​

once(type: string, listener?: Listener): Promise<any> | GeoJSONSource

Defined in: src/util/evented.ts:106

Adds a listener that will be called only once to a specified event type.

The listener will be called first time the event fires after the listener is registered.

Parameters ​

ParameterTypeDescription
typestringThe event type to listen for.
listener?ListenerThe function to be called when the event is fired the first time.

Returns ​

Promise<any> | GeoJSONSource

this or a promise if a listener is not provided

Inherited from ​

Evented.once


onRemove() ​

onRemove(): void

Defined in: src/source/geojson_source.ts:469

This method is called when the source is removed from the map.

Returns ​

void

Implementation of ​

Source.onRemove


serialize() ​

serialize(): GeoJSONSourceSpecification

Defined in: src/source/geojson_source.ts:474

Returns ​

GeoJSONSourceSpecification

A plain (stringifiable) JS object representing the current state of the source. Creating a source using the returned object as the options should result in a Source that is equivalent to this one.

Implementation of ​

Source.serialize


setClusterOptions() ​

setClusterOptions(options: SetClusterOptions): this

Defined in: src/source/geojson_source.ts:307

To disable/enable clustering on the source options

Parameters ​

ParameterTypeDescription
optionsSetClusterOptionsThe options to set

Returns ​

this

Example ​

ts
map.getSource('some id').setClusterOptions({cluster: false});
map.getSource('some id').setClusterOptions({cluster: false, clusterRadius: 50, clusterMaxZoom: 14});

setData() ​

setData(data: string | GeoJSON<Geometry, {[name: string]: any; }>): this

Defined in: src/source/geojson_source.ts:225

Sets the GeoJSON data and re-renders the map.

Parameters ​

ParameterTypeDescription
datastring | GeoJSON<Geometry, {[name: string]: any; }>A GeoJSON data object or a URL to one. The latter is preferable in the case of large GeoJSON files.

Returns ​

this


setEventedParent() ​

setEventedParent(parent?: Evented, data?: any): GeoJSONSource

Defined in: src/util/evented.ts:176

Bubble all events fired by this instance of Evented to this parent instance of Evented.

Parameters ​

ParameterType
parent?Evented
data?any

Returns ​

GeoJSONSource

Inherited from ​

Evented.setEventedParent


unloadTile() ​

unloadTile(tile: Tile): Promise<void>

Defined in: src/source/geojson_source.ts:464

Allows to unload a tile.

Parameters ​

ParameterTypeDescription
tileTileThe tile to unload

Returns ​

Promise<void>

Implementation of ​

Source.unloadTile


updateData() ​

updateData(diff: GeoJSONSourceDiff): this

Defined in: src/source/geojson_source.ts:246

Updates the source's GeoJSON, and re-renders the map.

For sources with lots of features, this method can be used to make updates more quickly.

This approach requires unique IDs for every feature in the source. The IDs can either be specified on the feature, or by using the promoteId option to specify which property should be used as the ID.

It is an error to call updateData on a source that did not have unique IDs for each of its features already.

Updates are applied on a best-effort basis, updating an ID that does not exist will not result in an error.

Parameters ​

ParameterTypeDescription
diffGeoJSONSourceDiffThe changes that need to be applied.

Returns ​

this

Properties ​

attribution ​

attribution: string

Defined in: src/source/geojson_source.ts:117

The attribution for the source.

Implementation of ​

Source.attribution


id ​

id: string

Defined in: src/source/geojson_source.ts:113

The id for the source. Must not be used by any existing source.

Implementation of ​

Source.id


isTileClipped ​

isTileClipped: boolean

Defined in: src/source/geojson_source.ts:120

false if tiles can be drawn outside their boundaries, true if they cannot.

Implementation of ​

Source.isTileClipped


maxzoom ​

maxzoom: number

Defined in: src/source/geojson_source.ts:115

The maximum zoom level for the source.

Implementation of ​

Source.maxzoom


minzoom ​

minzoom: number

Defined in: src/source/geojson_source.ts:114

The minimum zoom level for the source.

Implementation of ​

Source.minzoom


reparseOverscaled ​

reparseOverscaled: boolean

Defined in: src/source/geojson_source.ts:121

true if tiles should be sent back to the worker for each overzoomed zoom level, false if not.

Implementation of ​

Source.reparseOverscaled


tileSize ​

tileSize: number

Defined in: src/source/geojson_source.ts:116

The tile size for the source.

Implementation of ​

Source.tileSize