---
title: "useMapboxCamera"
description: "Camera helper wrapping flyTo, easeTo, jumpTo and fitBounds; all methods await map load and fitBounds accepts any GeoJSON."
canonical_url: "https://mapbox.mhaibaraai.cn/en/docs/composables/use-mapbox-camera"
---
# useMapboxCamera

> Camera helper wrapping flyTo, easeTo, jumpTo and fitBounds; all methods await map load and fitBounds accepts any GeoJSON.

## Introduction

`useMapboxCamera` wraps common camera operations: smooth fly-to, easing, instant jump, and fit-to-bounds. All methods return a `Promise` and internally await the map's loaded state before executing, so there is no need to manually check readiness. `fitBounds` additionally accepts any GeoJSON and automatically computes its bounding box.

> [!NOTE]
> 
> When used outside a 
> 
> <MapboxMap>
> 
>  subtree, specify the target map via 
> 
> options.mapId
> 
> .

## Usage

A toolbar triggers fly-to and fit-to-route (`fitBounds` receives a GeoJSON directly):

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

const mapId = 'use-mapbox-camera-demo'

const route: FeatureCollection = {
  type: 'FeatureCollection',
  features: [
    {
      type: 'Feature',
      properties: {},
      geometry: {
        type: 'LineString',
        coordinates: [
          [116.39, 39.91],
          [117.20, 39.13],
          [118.18, 39.63],
          [119.60, 39.94]
        ]
      }
    }
  ]
}

const { flyTo, fitBounds } = useMapboxCamera({ mapId })
</script>

<template>
  <div class="relative h-115 w-full overflow-hidden rounded-(--ui-radius) border border-default">
    <MapboxMap :map-id="mapId" :options="{ style: 'mapbox://styles/mapbox/light-v11', center: [117.5, 39.6], zoom: 6 }">
      <MapboxLayer
        layer-id="route"
        type="line"
        :source="{ type: 'geojson', data: route }"
        :layout="{ 'line-cap': 'round', 'line-join': 'round' }"
        :paint="{ 'line-color': '#8b5cf6', 'line-width': 4 }"
      />
    </MapboxMap>
    <div class="absolute left-2 top-2 z-10 flex gap-1">
      <UButton size="sm" @click="fitBounds(route, { padding: 60 })">
        缩放到路线
      </UButton>
      <UButton size="sm" variant="soft" @click="flyTo({ center: [116.39, 39.91], zoom: 11, pitch: 45 })">
        飞向北京
      </UButton>
    </div>
  </div>
</template>
```

## API

### `useMapboxCamera()`

Creates a camera helper.

**options.mapId** (`string`): Target map id; required when used outside a <MapboxMap> subtree.

Returns `UseMapboxCameraReturn`:

**flyTo** (`(options: EasingOptions) => Promise<void>`): Smoothly flies to the target camera position.

**easeTo** (`(options: EasingOptions) => Promise<void>`): Eases to the target camera position.

**jumpTo** (`(options: CameraOptions) => Promise<void>`): Immediately jumps to the target camera position.

**fitBounds** (`(target: LngLatBoundsLike | GeoJSON, options?: FitBoundsOptions) => Promise<void>`): Fits the viewport to the target bounds; target accepts a bounds object or any GeoJSON (bounding box is computed automatically).

## Changelog

See commit history for [src/runtime/composables/useMapboxCamera.ts](https://github.com/mhaibaraai/movk-mapbox/commits/main/src/runtime/composables/useMapboxCamera.ts).


## Sitemap

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