---
title: "MapboxSource"
description: "A declarative data source supporting geojson / vector / raster / image / video, with incremental updates by type and the ability to be shared across multiple layers."
canonical_url: "https://mapbox.mhaibaraai.cn/en/docs/core/source"
---
# MapboxSource

> A declarative data source supporting geojson / vector / raster / image / video, with incremental updates by type and the ability to be shared across multiple layers.

## Introduction

`MapboxSource` declaratively manages a mapbox-gl data source: it calls `addSource` when the map is ready, applies incremental updates when `source` changes (e.g. `setData` for geojson, `setTiles` for vector) instead of recreating the whole source, and safely removes it on unmount. Child layers reference the source by its `source="<sourceId>"` string.

> [!TIP]
> See: /docs/core/layer
> 
> A single source can be consumed by multiple layers simultaneously — this is the key advantage of an explicit 
> 
> MapboxSource
> 
>  over an inline source inside 
> 
> MapboxLayer
> 
> .

## Usage

One geojson source driving both a halo and a filled circle layer at the same time:

```vue [SourceGeojsonExample.vue]
<script setup lang="ts">
import type { FeatureCollection } from 'geojson'

// 同一数据源同时供「实心圆」与「光晕」两个图层消费
const data: FeatureCollection = {
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [116.397, 39.908] } },
    { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [116.45, 39.93] } },
    { type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: [116.35, 39.88] } }
  ]
}
</script>

<template>
  <div class="h-115 w-full overflow-hidden rounded-(--ui-radius) border border-default">
    <MapboxMap :options="{ style: 'mapbox://styles/mapbox/light-v11', center: [116.4, 39.9], zoom: 11 }">
      <MapboxSource source-id="cities" :source="{ type: 'geojson', data }">
        <MapboxLayer
          layer-id="cities-halo"
          type="circle"
          source="cities"
          :paint="{ 'circle-radius': 18, 'circle-color': '#3b82f6', 'circle-opacity': 0.2 }"
        />
        <MapboxLayer
          layer-id="cities-core"
          type="circle"
          source="cities"
          :paint="{ 'circle-radius': 7, 'circle-color': '#3b82f6' }"
        />
      </MapboxSource>
    </MapboxMap>
  </div>
</template>
```

## Examples

### Reactive Updates

`source` is reactive: switching datasets only triggers `setData` — no layers are rebuilt:

```vue [SourceUpdateExample.vue]
<script setup lang="ts">
import type { FeatureCollection } from 'geojson'

function randomPoints(count: number, seed: number): FeatureCollection {
  return {
    type: 'FeatureCollection',
    features: Array.from({ length: count }, (_, i) => ({
      type: 'Feature',
      properties: {},
      geometry: {
        type: 'Point',
        coordinates: [116.2 + ((i * seed) % 40) / 100, 39.8 + ((i * seed * 7) % 25) / 100]
      }
    }))
  }
}

const datasets = [randomPoints(12, 13), randomPoints(40, 31)]
const index = ref(0)

// source 为响应式：切换数据集仅触发 setData 增量更新，不重建源
const source = computed(() => ({ type: 'geojson' as const, data: datasets[index.value]! }))
</script>

<template>
  <div class="h-115 w-full overflow-hidden rounded-(--ui-radius) border border-default">
    <MapboxMap :options="{ style: 'mapbox://styles/mapbox/light-v11', center: [116.4, 39.9], zoom: 10 }">
      <div class="absolute left-3 top-3 z-10">
        <UButton size="xs" color="neutral" variant="solid" @click="index = index === 0 ? 1 : 0">
          Toggle dataset
        </UButton>
      </div>
      <MapboxSource source-id="pts" :source="source">
        <MapboxLayer
          layer-id="pts"
          type="circle"
          source="pts"
          :paint="{ 'circle-radius': 6, 'circle-color': '#3b82f6', 'circle-opacity': 0.8 }"
        />
      </MapboxSource>
    </MapboxMap>
  </div>
</template>
```

## API

### Props

```ts
/**
 * Props for the MapboxSource component
 */
interface MapboxSourceProps {
  /**
   * 数据源 id，供图层经 source 字段按字符串引用
   */
  sourceId: string;
  /**
   * 数据源定义
   */
  source: mapboxgl.SourceSpecification;
}
```

### Slots

```ts
/**
 * Slots for the MapboxSource component
 */
interface MapboxSourceSlots {
  default(): any;
}
```

## Changelog

See commit history for [src/runtime/components/MapboxSource.vue](https://github.com/mhaibaraai/movk-mapbox/commits/main/src/runtime/components/MapboxSource.vue).


## Sitemap

See the full [sitemap](https://mapbox.mhaibaraai.cn/sitemap.md) for all pages.
