---
title: "MapboxLayerGroup"
description: "A layer group that provides a shared insertion anchor and a single group-level visibility toggle for its child layers."
canonical_url: "https://mapbox.mhaibaraai.cn/en/docs/core/layer-group"
---
# MapboxLayerGroup

> A layer group that provides a shared insertion anchor and a single group-level visibility toggle for its child layers.

## Introduction

`MapboxLayerGroup` organizes multiple child [MapboxLayer](https://mapbox.mhaibaraai.cn/docs/core/layer) components into a group: `beforeId` provides a shared default insertion anchor for all children, and `visible` acts as a group-level visibility toggle (the group switch takes precedence over each child layer's own `layout.visibility`). This is ideal for scenarios where a set of related layers should be shown or hidden together.

## Usage

Toggling `visible` shows or hides the entire group — no need to update each layer individually:

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

const visible = ref(true)

const data: FeatureCollection = {
  type: 'FeatureCollection',
  features: [{
    type: 'Feature',
    properties: {},
    geometry: {
      type: 'Polygon',
      coordinates: [[[116.36, 39.95], [116.44, 39.95], [116.44, 39.89], [116.36, 39.89], [116.36, 39.95]]]
    }
  }]
}
</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.92], zoom: 11 }">
      <div class="absolute left-3 top-3 z-10">
        <UButton size="xs" color="neutral" variant="solid" @click="visible = !visible">
          {{ visible ? 'Hide group' : 'Show group' }}
        </UButton>
      </div>
      <!-- 组级 visible 统一开关子图层，无需逐层设置 layout.visibility -->
      <MapboxLayerGroup :visible="visible">
        <MapboxLayer
          layer-id="zone-fill"
          type="fill"
          :source="{ type: 'geojson', data }"
          :paint="{ 'fill-color': '#f43f5e', 'fill-opacity': 0.3 }"
        />
        <MapboxLayer
          layer-id="zone-line"
          type="line"
          :source="{ type: 'geojson', data }"
          :paint="{ 'line-color': '#f43f5e', 'line-width': 2 }"
        />
      </MapboxLayerGroup>
    </MapboxMap>
  </div>
</template>
```

## API

### Props

```ts
/**
 * Props for the MapboxLayerGroup component
 */
interface MapboxLayerGroupProps {
  /**
   * 组内图层缺省插入到该图层之前
   */
  beforeId?: string | undefined;
  /**
   * 组级显隐
   * @default "true"
   */
  visible?: boolean | undefined;
}
```

### Slots

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

## Changelog

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


## Sitemap

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