You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ArcGIS JavaScript API中将Layer List放入Expand组件?

将Layer List添加到ArcGIS JS API的Expand组件中

实现逻辑和你已有的Basemap Gallery完全一致,核心是将LayerList实例赋值给Expand组件的content属性即可。以下是完整实现代码:

1. 导入所需模块(ES模块方式)

import LayerList from "@arcgis/core/widgets/LayerList";
import Expand from "@arcgis/core/widgets/Expand";

2. 创建LayerList实例

const layerList = new LayerList({
  view: view,
  // 可选配置:自定义图层项,比如添加图例、控制排序等
  listItemCreatedFunction: (event) => {
    const item = event.item;
    // 为每个图层项添加可展开的图例面板
    item.panel = {
      content: "legend",
      open: false
    };
    // 可选:禁止图层排序
    item.actionsSections = [];
  }
});

3. 创建Expand组件并关联LayerList

const layerListExpand = new Expand({
  view: view,
  content: layerList,
  // 自定义Expand的标题和内置图标
  title: "图层列表",
  icon: "layers"
});

4. 添加Expand到视图UI

view.ui.add(layerListExpand, "top-right");

可选:移动端自动折叠逻辑

如果需要像Basemap Gallery那样,在移动端操作图层后自动折叠Expand,可添加以下监听:

// 监听图层列表项变化,移动端自动折叠
layerList.watch("items", () => {
  const isMobile = view.heightBreakpoint === "xsmall" || view.widthBreakpoint === "xsmall";
  if (isMobile) {
    layerListExpand.collapse();
  }
});

关键说明

ArcGIS JS API的所有Widget(包括LayerList、BasemapGallery)都可以直接作为Expand组件的content值,实现方式完全复用你已有的Basemap Gallery逻辑,仅需替换Widget实例即可。

内容的提问来源于stack exchange,提问作者user21045033

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 08:02:16