ArcGIS JS 基础教程(17):GraphicsLayer 图形图层(点线面、Mesh)

简介: ArcGIS JS 基础教程(17):GraphicsLayer 图形图层(点线面、Mesh)

零、写在前面

📌 本系列教程完整目录:ArcGIS JS 系列基础教程(100个项目常用热门功能)

💡 在线示例:完整可运行的 HTML 示例,无需任何环境配置,可直接在浏览器中打开体验
🗂️ 专栏导航:收藏 + 关注,专栏文章第一时间送达
❤️ 一键三连:点赞(给教程充电)+ 评论(提问必回)+ 收藏(下次再看)


一、功能介绍

GraphicsLayer 是 ArcGIS JS 中客户端临时图形图层,用于动态添加点、线、面、3D Mesh、文本等图形标记。数据仅存于浏览器内存,不与服务器交互,是搜索高亮、测量标注、实时轨迹等场景的首选。


二、功能实现

核心 API: GraphicsLayer (@arcgis/core/layers/GraphicsLayer.js) + Graphic。

2.1 创建与添加图形

const layer = new GraphicsLayer({
    title: "标记层", elevationInfo: {
    mode: "relative-to-ground" } });
const graphic = new Graphic({
   
    geometry: new Point({
    longitude: 116.39, latitude: 39.90, z: 150 }),
    symbol: {
    type: "point-3d", symbolLayers: [{
    type: "icon", resource: {
    primitive: "circle" }, size: 12, material: {
    color: [255,0,0] } }] }
});
layer.add(graphic); map.add(layer);

elevationInfo: { mode: "relative-to-ground" } 让图形的 z 值成为相对地面的偏移。设为 z: 150 则浮空 150 米;设为 z: 0(默认)则贴地。

2.2 自定义 Mesh(顶点 + 索引构建)

除 Mesh.createBox() / createSphere() 等预制体外,还可以通过 vertexAttributes 和 components 手动构建任意自定义网格:

const position = new Float64Array([
    116.397, 39.9174, 250,   // apex
    116.3965, 39.9165, 100,  // base-left
    116.3975, 39.9165, 100,  // base-right
    116.397,  39.9175, 100   // base-front
]);
const faces = new Uint32Array([
    0, 1, 2,  0, 2, 3,  0, 3, 1,  1, 3, 2
]);
gl.add(new Graphic({
   
    geometry: new Mesh({
   
        vertexAttributes: {
    position: position },
        components: [{
    faces: faces }],
        spatialReference: {
    wkid: 4326 }
    }),
    symbol: {
    type: "mesh-3d", symbolLayers: [{
    type: "fill", material: {
    color: [255,100,50,0.85] } }] }
}));

2.3 3D 符号类型

几何 符号 说明
Point point-3d + icon/object 3D球/锥/图标
Polyline line-3d 3D线条
Polygon polygon-3d + fill/extrude 面填充/拉伸
Mesh mesh-3d + fill 自定义网格

2.4 常用操作

layer.removeAll();          // 清空所有图形
layer.addMany([g1, g2]);   // 批量添加
graphic.geometry = newPoint;// 更新几何
graphic.symbol = newSymbol; // 更新符号

三、功能应用

场景 说明
搜索结果高亮 点击列表→地图上标记位置
测量标注 绘制测量线/面标注距离面积
实时轨迹 定时更新 Graphic.geometry 实现移动动画

四、核心代码

📦 sample/lesson17_graphics_layer.html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>第17课:GraphicsLayer 图形图层</title>
    <link rel="stylesheet" href="https://js.arcgis.com/5.0/esri/themes/light/main.css">
    <script type="module" src="https://js.arcgis.com/5.0/"></script>
    <style>
        * {
     margin: 0; padding: 0; box-sizing: border-box; }
        body {
     font-family: "Microsoft YaHei", sans-serif; }
        #mapContainer {
     width: 100vw; height: 100vh; }
        .page-title {
    
            position: absolute; top: 20px; left: 50%; transform: translateX(-50%);
            background: rgba(255,255,255,0.95); padding: 10px 24px; border-radius: 6px;
            font-size: 18px; font-weight: bold; z-index: 100;
            box-shadow: 0 2px 8px rgba(0,0,0,0.15);
        }
        .panel {
    
            position: absolute; top: 80px; right: 20px;
            background: rgba(255,255,255,0.95); padding: 16px; border-radius: 8px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.15);
            z-index: 100; min-width: 260px;
        }
        .panel h3 {
     margin: 0 0 8px 0; font-size: 14px; }
        .btn-row {
     display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }
        .btn-row button {
    
            flex: 1; padding: 6px 0;
            border: 1px solid #d9d9d9; border-radius: 4px;
            background: white; cursor: pointer; font-size: 12px;
        }
        .btn-row button:hover {
     border-color: #1890ff; color: #1890ff; }
        .status {
    
            position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
            background: rgba(0,0,0,0.7); color: white; padding: 8px 20px;
            border-radius: 20px; font-size: 13px; z-index: 100; pointer-events: none;
        }
    </style>
</head>
<body>
<h1 class="page-title">第17课:GraphicsLayer 图形图层</h1>

<div class="panel">
    <h3>📍 添加图形</h3>
    <div class="btn-row">
        <button id="btnPoint">🔴 加点</button>
        <button id="btnLine">📏 加线</button>
        <button id="btnPolygon">🔷 加面</button>
    </div>
    <div class="btn-row">
        <button id="btnMesh">📦 加立方体</button>
        <button id="btnCustomMesh">🔺 自定义Mesh</button>
    </div>
    <div class="btn-row">
        <button id="btnClear">🗑 清除</button>
    </div>
</div>

<div class="status" id="st">GraphicsLayer | 点击按钮添加图形</div>
<div id="mapContainer"></div>

<script type="module">
    const Map = await $arcgis.import("@arcgis/core/Map.js");
    const SceneView = await $arcgis.import("@arcgis/core/views/SceneView.js");
    const GraphicsLayer = await $arcgis.import("@arcgis/core/layers/GraphicsLayer.js");
    const Graphic = await $arcgis.import("@arcgis/core/Graphic.js");
    const Point = await $arcgis.import("@arcgis/core/geometry/Point.js");
    const Polyline = await $arcgis.import("@arcgis/core/geometry/Polyline.js");
    const Polygon = await $arcgis.import("@arcgis/core/geometry/Polygon.js");
    const Mesh = await $arcgis.import("@arcgis/core/geometry/Mesh.js");
    const ElevationLayer = await $arcgis.import("@arcgis/core/layers/ElevationLayer.js");
    const getTianditu = await $arcgis.import("https://openlayers.vip/examples/resources/tianditu.js");

    const tl = getTianditu.default({
     type: "vec_w" });
    const map = new Map({
    
        basemap: {
     baseLayers: [tl.base, tl.anno] },
        ground: {
    
            surface: {
    
                elevationLayers: [new ElevationLayer({
    
                    url: "https://www.geosceneonline.cn/image/rest/services/OpenData/ChinaTerrain3D/ImageServer/"
                })]
            }
        }
    });

    const view = new SceneView({
    
        container: "mapContainer", map: map,
        camera: {
     position: {
     longitude: 116.397, latitude: 39.917, z: 800 }, heading: 30, tilt: 60 }
    });

    const gl = new GraphicsLayer({
    
        title: "图形层",
        elevationInfo: {
     mode: "relative-to-ground" }
    });
    map.add(gl);

    function S(msg) {
     document.getElementById("st").textContent = msg; }
    const c = {
     lng: 116.397, lat: 39.917 };

    // 添加点
    document.getElementById("btnPoint").onclick = () => {
    
        const g = new Graphic({
    
            geometry: new Point({
    
                longitude: c.lng + 0.002 * Math.random(),
                latitude: c.lat + 0.002 * Math.random(),
                z: 150
            }),
            symbol: {
    
                type: "point-3d",
                symbolLayers: [{
     type: "icon", resource: {
     primitive: "circle" }, size: 16, material: {
     color: [255, 50, 50] } }]
            }
        });
        gl.add(g);
        view.goTo(g.geometry);
        S("已添加红点(浮空 150m)");
    };

    // 添加线
    document.getElementById("btnLine").onclick = () => {
    
        const g = new Graphic({
    
            geometry: new Polyline({
    
                paths: [[[c.lng - 0.003, c.lat - 0.002], [c.lng + 0.001, c.lat + 0.001], [c.lng + 0.003, c.lat - 0.001]]]
            }),
            symbol: {
     type: "line-3d", symbolLayers: [{
     type: "line", size: 4, material: {
     color: [255, 165, 0] } }] }
        });
        gl.add(g);
        view.goTo(g.geometry);
        S("已添加橙色线");
    };

    // 添加面
    document.getElementById("btnPolygon").onclick = () => {
    
        const g = new Graphic({
    
            geometry: new Polygon({
    
                rings: [[[c.lng - 0.002, c.lat - 0.001], [c.lng + 0.002, c.lat - 0.001], [c.lng + 0.001, c.lat + 0.002], [c.lng - 0.002, c.lat + 0.001]]]
            }),
            symbol: {
    
                type: "polygon-3d",
                symbolLayers: [{
     type: "fill", material: {
     color: [0, 120, 255, 0.3] }, outline: {
     color: [0, 120, 255, 0.8] } }]
            }
        });
        gl.add(g);
        view.goTo(g.geometry);
        S("已添加蓝色面");
    };

    // 添加3D立方体
    document.getElementById("btnMesh").onclick = () => {
    
        const g = new Graphic({
    
            geometry: Mesh.createBox(new Point({
     longitude: c.lng + 0.001, latitude: c.lat + 0.002, z: 0 }), {
     size: {
     width: 100, height: 150, depth: 100 } }),
            symbol: {
     type: "mesh-3d", symbolLayers: [{
     type: "fill", material: {
     color: [0, 200, 100, 0.9] } }] }
        });
        gl.add(g);
        view.goTo(g.geometry);
        S("已添加绿色立方体");
    };

    // 添加自定义Mesh(四棱锥:顶点+索引构建)
    document.getElementById("btnCustomMesh").onclick = () => {
    
        const cx = c.lng, cy = c.lat, s = 0.0005; // ~55m
        const position = new Float64Array([
            cx,         cy + s * 0.8, 250,  // apex
            cx - s,     cy - s * 0.5, 100,  // base-left
            cx + s,     cy - s * 0.5, 100,  // base-right
            cx,         cy + s,       100   // base-front
        ]);
        const faces = new Uint32Array([
            0, 1, 2,  0, 2, 3,  0, 3, 1,  1, 3, 2
        ]);
        const g = new Graphic({
    
            geometry: new Mesh({
    
                vertexAttributes: {
     position: position },
                components: [{
     faces: faces }],
                spatialReference: {
     wkid: 4326 }
            }),
            symbol: {
    
                type: "mesh-3d",
                symbolLayers: [{
     type: "fill", material: {
     color: [255, 100, 50, 0.85] } }]
            }
        });
        gl.add(g);
        view.goTo(g.geometry);
        S("已添加自定义四棱锥 Mesh(顶点+索引构建)");
    };

    // 清除
    document.getElementById("btnClear").onclick = () => {
     gl.removeAll(); S("图形已清除"); };
</script>
</body>
</html>

五、在线示例

🔗 在线体验:https://southjor.github.io/arcgis-examples/lessons/lesson17.html

在这里插入图片描述

操作说明:

  1. 场景加载后相机贴近地面(z:800),方便观察图形添加效果
  2. 点击「🔴 加点」:红点浮空 150m(z:150 + relative-to-ground),自动 goTo 定位
  3. 点击「📏 加线」「🔷 加面」「📦 加立方体」添加基本图形
  4. 点击「🔺 自定义Mesh」:通过 vertexAttributes + components.faces 构建的四棱锥
  5. 点击「🗑 清除」移除所有图形

六、关键 API 说明

API 说明
new GraphicsLayer({ elevationInfo }) 创建图形图层
new Graphic({ geometry, symbol }) 创建图形
layer.add(graphic) 添加单个
layer.addMany([...]) 批量添加
layer.removeAll() 清空

参考链接: GraphicsLayer API


七、系列导航

⬅️ 上一篇:ArcGIS JS 基础教程(16):禁用视图交互

➡️ 下一篇:ArcGIS JS 基础教程(18):SceneLayer 场景图层


💡 小贴士:GraphicsLayer 是纯客户端图层,图形只存在于浏览器内存中、不与服务器交互,因此非常适合搜索高亮、测量标注、实时轨迹等「临时标记」场景。如果要展示大量、需要持久化或查询的要素,请改用 FeatureLayer。另外 3D 场景里建议显式设置 elevationInfo,明确图形的放置方式(贴地/相对地面/绝对高度),避免位置不符合预期。

相关文章
|
16天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
15446 52
|
5天前
|
域名解析 人工智能 Linux
|
14天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
11天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
1922 5
|
16天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3494 22
|
4天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
959 0
|
17天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。
|
14天前
|
传感器 安全 区块链
Windows 电脑运行安卓应用教程:开源工具 ApkStart 双击 APK 即装、独立窗口运行
ApkStart 是一款开源Windows工具,让安卓APP像原生软件一样运行:双击APK即安装、生成真实图标、独立窗口启动、开始菜单可搜索。无需传统模拟器,内置QEMU运行时,支持数据互通、ARM兼容与命令行操作。(239字)
|
14天前
|
人工智能 弹性计算 开发工具
零成本玩各大模型!DeepSeek Harness dsh-our-free-model 插件,13 个厂商模型直接用
总结一下:装这个插件,你不用注册、不用绑卡、不用填一个 Key,就能在 DeepSeek Harness 里用上 DeepSeek V4.1 Flash、Kimi K3 这一批前沿模型,而且是白嫖的、还不限量。三步搞定:下载插件,复制包含 `package.json` 的插件目录路径,在「添加插件」里粘贴路径安装并重启。无论是在阿里云 ECS 上通过命令行部署的 Web 版,还是本地的 Electron 桌面端,都能顺利跑通,快速搭建一套零成本的免费模型使用环境。
2229 1

热门文章

最新文章