MapboxLayerGroup

View source
图层组,为子图层提供统一的插入锚点与组级显隐开关。

简介

MapboxLayerGroup 把多个子 MapboxLayer 归为一组:经 beforeId 提供统一的缺省插入锚点,经 visible 做组级显隐开关(组开关优先于子图层自身的 layout.visibility)。适合「一组相关图层一起显示/隐藏」的场景。

用法

切换 visible 即可整组开关,无需逐层设置可见性:

<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

Prop Default Type
beforeIdstring

组内图层缺省插入到该图层之前

visibletrueboolean

组级显隐

Slots

Slot Type
default{}

Changelog

No recent changes
Copyright © 2026 - 2026 YiXuan - MIT License