---
title: "MapboxDrawControl"
description: "Declarative drawing control built on mapbox-gl-draw, with v-model features and mode plus imperative instance methods."
canonical_url: "https://mapbox.mhaibaraai.cn/en/docs/extensions/draw"
---
# MapboxDrawControl

> Declarative drawing control built on mapbox-gl-draw, with v-model features and mode plus imperative instance methods.

## Introduction

`MapboxDrawControl` provides declarative drawing on top of [mapbox-gl-draw](https://github.com/mapbox/mapbox-gl-draw): `v-model:features` controls the feature collection (assigning imports, drawing writes back), `v-model:mode` controls the current mode; it emits create / update / delete / selectionchange / modechange events; and exposes imperative methods via `defineExpose`. Child components can inject the draw context with [useMapboxDraw](https://mapbox.mhaibaraai.cn/docs/composables/use-mapbox-draw); once the parent map sets a `map-id`, that composable can also drive drawing from outside the component tree.

## Usage

Draw points, lines, and polygons with the built-in toolbar — `v-model:features` reflects the live feature count:

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

const features = ref<Feature[]>([])
</script>

<template>
  <div class="relative h-115 w-full overflow-hidden rounded-(--ui-radius) border border-default">
    <MapboxMap :options="{ style: 'mapbox://styles/mapbox/light-v11', center: [116.397, 39.908], zoom: 11 }">
      <MapboxDrawControl v-model:features="features" position="top-left" />
    </MapboxMap>
    <div class="absolute top-2 right-2 z-10 rounded bg-default/90 px-2 py-1 text-xs text-default ring ring-default">
      已绘制 {{ features.length }} 个要素
    </div>
  </div>
</template>
```

## Examples

### Imperative Actions

Access the instance via [`useTemplateRef`](https://vuejs.org/api/composition-api-helpers.html#usetemplateref) to call `changeMode` / `deleteAll` (default toolbar is disabled):

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

const features = ref<Feature[]>([])
const drawRef = useTemplateRef('drawRef')

function draw(mode: string) {
  drawRef.value?.changeMode(mode)
}
</script>

<template>
  <div class="flex h-115 w-full flex-col gap-2">
    <div class="flex flex-wrap items-center gap-2">
      <UButton size="xs" color="neutral" variant="subtle" @click="draw('draw_point')">
        点
      </UButton>
      <UButton size="xs" color="neutral" variant="subtle" @click="draw('draw_line_string')">
        线
      </UButton>
      <UButton size="xs" color="neutral" variant="subtle" @click="draw('draw_polygon')">
        面
      </UButton>
      <UButton size="xs" color="error" variant="subtle" @click="drawRef?.deleteAll()">
        清空
      </UButton>
      <span class="ml-auto text-xs text-muted">{{ features.length }} 个要素</span>
    </div>
    <div class="relative flex-1 overflow-hidden rounded-(--ui-radius) border border-default">
      <MapboxMap :options="{ style: 'mapbox://styles/mapbox/light-v11', center: [116.397, 39.908], zoom: 11 }">
        <MapboxDrawControl ref="drawRef" v-model:features="features" :options="{ displayControlsDefault: false }" />
      </MapboxMap>
    </div>
  </div>
</template>
```

## API

### Props

```ts
/**
 * Props for the MapboxDrawControl component
 */
interface MapboxDrawControlProps {
  /**
   * 控件停靠位置；省略用地图默认位置
   */
  position?: mapboxgl.ControlPosition | undefined;
  /**
   * MapboxDraw 构造选项
   */
  options?: MapboxDraw.MapboxDrawOptions | undefined;
  features?: GeoJSON.Feature<GeoJSON.Geometry, GeoJSON.GeoJsonProperties>[] | undefined;
  mode?: string | undefined;
}
```

### Emits

```ts
/**
 * Emitted events for the MapboxDrawControl component
 */
interface MapboxDrawControlEmits {
  selectionchange: (payload: [features: GeoJSON.Feature<GeoJSON.Geometry, GeoJSON.GeoJsonProperties>[]]) => void;
  update:features: (payload: [value: GeoJSON.Feature<GeoJSON.Geometry, GeoJSON.GeoJsonProperties>[] | undefined]) => void;
  update:mode: (payload: [value: string | undefined]) => void;
  create: (payload: [features: GeoJSON.Feature<GeoJSON.Geometry, GeoJSON.GeoJsonProperties>[]]) => void;
  update: (payload: [features: GeoJSON.Feature<GeoJSON.Geometry, GeoJSON.GeoJsonProperties>[]]) => void;
  delete: (payload: [features: GeoJSON.Feature<GeoJSON.Geometry, GeoJSON.GeoJsonProperties>[]]) => void;
  modechange: (payload: [mode: string]) => void;
}
```

### Slots

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

### Expose

Access the component instance via [`useTemplateRef`](https://vuejs.org/api/composition-api-helpers.html#usetemplateref).

<table>
<thead>
  <tr>
    <th>
      Name
    </th>
    
    <th>
      Type
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          draw
        </span>
      </code>
    </td>
    
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          Readonly
        </span>
        
        <span class="sMK4o">
          <
        </span>
        
        <span class="sBMFI">
          Ref
        </span>
        
        <span class="sMK4o">
          <
        </span>
        
        <span class="sBMFI">
          MapboxDraw
        </span>
        
        <span class="sMK4o">
          |
        </span>
        
        <span class="sBMFI">
          undefined
        </span>
        
        <span class="sMK4o">
          >>
        </span>
      </code>
      
       <br />
      
       <p>
        Reference to the underlying MapboxDraw instance; undefined before mount
      </p>
    </td>
  </tr>
  
  <tr>
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          whenReady
        </span>
      </code>
    </td>
    
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          ()
        </span>
        
        <span class="spNyl">
          =>
        </span>
        
        <span class="sBMFI">
          Promise
        </span>
        
        <span class="sMK4o">
          <
        </span>
        
        <span class="sBMFI">
          MapboxDraw
        </span>
        
        <span class="sMK4o">
          >
        </span>
      </code>
      
       <br />
      
       <p>
        Resolves once the draw instance is ready
      </p>
    </td>
  </tr>
  
  <tr>
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          getAll
        </span>
      </code>
    </td>
    
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          ()
        </span>
        
        <span class="spNyl">
          =>
        </span>
        
        <span class="sBMFI">
          FeatureCollection
        </span>
        
        <span class="sMK4o">
          |
        </span>
        
        <span class="sBMFI">
          undefined
        </span>
      </code>
      
       <br />
      
       <p>
        Returns all current features as a collection
      </p>
    </td>
  </tr>
  
  <tr>
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          getMode
        </span>
      </code>
    </td>
    
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          ()
        </span>
        
        <span class="spNyl">
          =>
        </span>
        
        <span class="sBMFI">
          string
        </span>
        
        <span class="sMK4o">
          |
        </span>
        
        <span class="sBMFI">
          undefined
        </span>
      </code>
      
       <br />
      
       <p>
        Returns the current draw mode
      </p>
    </td>
  </tr>
  
  <tr>
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          add
        </span>
      </code>
    </td>
    
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          (
        </span>
        
        <span class="sHdIc">
          geojson
        </span>
        
        <span class="sMK4o">
          :
        </span>
        
        <span class="sBMFI">
          Feature
        </span>
        
        <span class="sMK4o">
          |
        </span>
        
        <span class="sBMFI">
          FeatureCollection
        </span>
        
        <span class="sMK4o">
          |
        </span>
        
        <span class="sBMFI">
          Geometry
        </span>
        
        <span class="sMK4o">
          )
        </span>
        
        <span class="spNyl">
          =>
        </span>
        
        <span class="sBMFI">
          Promise
        </span>
        
        <span class="sMK4o">
          <
        </span>
        
        <span class="sBMFI">
          string
        </span>
        
        <span class="sTEyZ">
          []
        </span>
        
        <span class="sMK4o">
          >
        </span>
      </code>
      
       <br />
      
       <p>
        Adds features, syncs the model, and returns their ID list
      </p>
    </td>
  </tr>
  
  <tr>
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          deleteAll
        </span>
      </code>
    </td>
    
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          ()
        </span>
        
        <span class="spNyl">
          =>
        </span>
        
        <span class="sBMFI">
          Promise
        </span>
        
        <span class="sMK4o">
          <
        </span>
        
        <span class="sBMFI">
          void
        </span>
        
        <span class="sMK4o">
          >
        </span>
      </code>
      
       <br />
      
       <p>
        Clears all features and syncs the model
      </p>
    </td>
  </tr>
  
  <tr>
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          changeMode
        </span>
      </code>
    </td>
    
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          (
        </span>
        
        <span class="sHdIc">
          mode
        </span>
        
        <span class="sMK4o">
          :
        </span>
        
        <span class="sBMFI">
          string
        </span>
        
        <span class="sMK4o">
          )
        </span>
        
        <span class="spNyl">
          =>
        </span>
        
        <span class="sBMFI">
          Promise
        </span>
        
        <span class="sMK4o">
          <
        </span>
        
        <span class="sBMFI">
          void
        </span>
        
        <span class="sMK4o">
          >
        </span>
      </code>
      
       <br />
      
       <p>
        Switches the draw mode and syncs the model
      </p>
    </td>
  </tr>
  
  <tr>
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sBMFI">
          setFeatureProperty
        </span>
      </code>
    </td>
    
    <td>
      <code className="language-ts-type shiki shiki-themes material-theme-lighter material-theme material-theme-palenight" language="ts-type" style="">
        <span class="sMK4o">
          (
        </span>
        
        <span class="sHdIc">
          featureId
        </span>
        
        <span class="sMK4o">
          :
        </span>
        
        <span class="sBMFI">
          string
        </span>
        
        <span class="sMK4o">
          ,
        </span>
        
        <span class="sHdIc">
          property
        </span>
        
        <span class="sMK4o">
          :
        </span>
        
        <span class="sBMFI">
          string
        </span>
        
        <span class="sMK4o">
          ,
        </span>
        
        <span class="sHdIc">
          value
        </span>
        
        <span class="sMK4o">
          :
        </span>
        
        <span class="sBMFI">
          unknown
        </span>
        
        <span class="sMK4o">
          )
        </span>
        
        <span class="spNyl">
          =>
        </span>
        
        <span class="sBMFI">
          Promise
        </span>
        
        <span class="sMK4o">
          <
        </span>
        
        <span class="sBMFI">
          void
        </span>
        
        <span class="sMK4o">
          >
        </span>
      </code>
      
       <br />
      
       <p>
        Sets a feature's user_* property (drives theme styles) and syncs the model
      </p>
    </td>
  </tr>
</tbody>
</table>

## Changelog

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

---

- [mapbox-gl-draw](https://github.com/mapbox/mapbox-gl-draw)


## Sitemap

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