ArcGIS JS 基础教程(6):地图弹窗信息窗口

简介: ArcGIS JS 基础教程(6):地图弹窗信息窗口

零、写在前面

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

💡 在线示例:完整可运行的 HTML 示例,无需任何环境配置,可直接在浏览器中打开体验
🗂️ 专栏导航:收藏 + 关注,专栏文章第一时间送达

❤️ 一键三连:点赞(给教程充电)+ 评论(提问必回)+ 收藏(下次再看)

一、功能介绍

弹窗(Popup)是 ArcGIS JS 中用于在地图上展示要素详细信息的交互式窗口。当用户点击地图上的某个要素(如建筑物、兴趣点、行政区划等)时,弹窗会显示该要素的属性信息,如名称、面积、人口、类型等。支持自定义模板、动态字段、附件查看等高级功能。

二、功能实现

使用 MapView 的 popup 属性配置弹窗模板,通过 popupTemplate 定义字段显示格式。关键步骤:

  1. 创建 Graphic 并设置 popupTemplate
  2. 将 Graphic 添加到 GraphicsLayer
  3. 配置 MapView.popup 启用点击触发弹窗

    三、功能应用

  4. 城市信息展示:点击建筑物显示建筑名称、楼层数、用途等信息。
  5. 兴趣点(POI)查询:点击餐厅、加油站等显示详细信息、评分、联系方式。
  6. 行政区划查询:点击省/市/区县显示区域面积、人口、GDP 等数据。
  7. 数据采集:弹窗内支持编辑和提交表单,实现现场数据采集。

    四、核心代码

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>第6课:地图弹窗信息窗口</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>
         body {
            margin: 0; padding: 0; 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);
         }
     </style>
    </head>
    <body>
    <h1 class="page-title">第6课:地图弹窗信息窗口</h1>
    <div id="mapContainer"></div>
    <script type="module">
     const Map = await $arcgis.import("@arcgis/core/Map.js");
     const MapView = await $arcgis.import("@arcgis/core/views/MapView.js");
     const Graphic = await $arcgis.import("@arcgis/core/Graphic.js");
     const GraphicsLayer = await $arcgis.import("@arcgis/core/layers/GraphicsLayer.js");
     const getTianditu = await $arcgis.import("https://openlayers.vip/examples/resources/tianditu.js");
     // 获取天地图底图
     const vecLayers = getTianditu.default({
            type: "vec_w" });
     // 创建地图
     const map = new Map({
           
         basemap: {
            baseLayers: [vecLayers.base, vecLayers.anno] }
     });
     // 创建MapView(2D视图,原生支持popup和constraints)
     const view = new MapView({
           
         container: "mapContainer",
         map: map,
         center: [116.4074, 39.9042],
         zoom: 12,
         popup: {
           
             dockEnabled: true,
             dockOptions: {
           
                 position: "bottom-left",
                 buttonEnabled: false,
                 collapseEnabled: false
             }
         }
     });
     // 创建GraphicsLayer存储要素
     const graphicsLayer = new GraphicsLayer();
     map.add(graphicsLayer);
     // 示例数据:北京主要地标
     const landmarks = [
         {
           
             name: "故宫博物院",
             type: "景区",
             level: "5A级",
             address: "北京市东城区景山前街4号",
             coords: [116.3975, 39.9182]
         },
         {
           
             name: "天安门广场",
             type: "广场",
             level: "国家级",
             address: "北京市东城区东长安街",
             coords: [116.3975, 39.9042]
         },
         {
           
             name: "国家大剧院",
             type: "文化设施",
             level: "特级",
             address: "北京市西城区西长安街2号",
             coords: [116.3917, 39.9045]
         },
         {
           
             name: "北京站",
             type: "交通枢纽",
             level: "特等站",
             address: "北京市东城区建国门内大街甲13号",
             coords: [116.4274, 39.9046]
         },
         {
           
             name: "三里屯太古里",
             type: "商业区",
             level: "市级",
             address: "北京市朝阳区三里屯路19号",
             coords: [116.4495, 39.9385]
         },
         {
           
             name: "中关村",
             type: "科技园区",
             level: "国家级",
             address: "北京市海淀区中关村大街",
             coords: [116.3126, 39.9831]
         }
     ];
     // 创建弹窗模板
     const createPopupTemplate = (name, type, level, address) => ({
           
         title: name,
         content: [
             {
           
                 type: "fields",
                 fieldInfos: [
                     {
           
                         fieldName: "type",
                         label: "类型",
                         visible: true,
                         format: {
            digitSeparator: false }
                     },
                     {
           
                         fieldName: "level",
                         label: "等级",
                         visible: true
                     },
                     {
           
                         fieldName: "address",
                         label: "地址",
                         visible: true
                     }
                 ]
             }
         ]
     });
     // 添加标注
     landmarks.forEach((landmark, index) => {
           
         const graphic = new Graphic({
           
             geometry: {
           
                 type: "point",
                 longitude: landmark.coords[0],
                 latitude: landmark.coords[1]
             },
             symbol: {
           
                 type: "simple-marker",
                 color: [230, 76, 60, 0.9],
                 outline: {
           
                     color: [255, 255, 255, 0.5],
                     width: 1
                 },
                 size: index < 3 ? 12 : 10
             },
             attributes: {
           
                 name: landmark.name,
                 type: landmark.type,
                 level: landmark.level,
                 address: landmark.address
             },
             popupTemplate: createPopupTemplate(
                 landmark.name,
                 landmark.type,
                 landmark.level,
                 landmark.address
             )
         });
         graphicsLayer.add(graphic);
     });
     // 默认弹出第一个点的信息
     view.when(() => {
           
         view.popup.open({
           
             location: {
            longitude: 116.3975, latitude: 39.9182 },
             title: landmarks[0].name,
             content: `类型:${
             landmarks[0].type}<br>等级:${
             landmarks[0].level}<br>地址:${
             landmarks[0].address}`
         });
         console.log("地图弹窗信息窗口示例加载完成!");
     });
    </script>
    </body>
    </html>
    

    五、在线示例

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

在这里插入图片描述

六、弹窗参数速查

参数 类型 说明 默认值
title String 弹窗标题(支持字段表达式) null
content Array/Object 弹窗内容(支持文本、字段、附件等) null
fieldInfos Array 字段显示配置(标签名、格式、可见性) null
actions Array 自定义操作按钮 null
dockEnabled Boolean 是否启用停靠模式 false
collapseEnabled Boolean 是否显示折叠按钮 true

七、系列导航

⬅️ 上一篇:ArcGIS JS 基础教程(5):地图限制缩放级别和显示范围

➡️ 下一篇:[ArcGIS JS 基础教程(7):Global与Local场景模式

](https://webgis.blog.csdn.net/article/details/161017592)

💬 有问题欢迎在评论区留言,我会第一时间回复!

📢 关注我的专栏,后续持续更新 ArcGIS JS 100 个常用功能教程

相关文章
|
云安全 安全 网络安全
|
5天前
|
JavaScript 安全 前端开发
Nodejs Brotli decoder 代码执行漏洞 (CVE-2026-111497)10629
Node.js发布安全更新修复Brotli解码器高危RCE漏洞(CVE-2026-111497),建议受影响版本用户立即升级。
1911 1
|
11天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
1845 38
|
8天前
|
Web App开发 人工智能 前端开发
2026 年 AI 浏览器怎么选?Ego Lite、Tabbit、Chrome DevTools MCP 深度对比,开发者到底该用哪个?
2026年AI浏览器选型指南:Ego Lite专注多Agent协同(Mac优先),Tabbit国产首选,原生支持Windows与Codex控制;Chrome DevTools MCP非浏览器而是开发者利器,深度集成Console/Network/Performance调试能力,让AI真正“看懂并验证”前端代码。
1195 3
|
5天前
|
安全 应用服务中间件 Go
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
PHP2Shell 双 RCE 已遭在野利用:WordPress、Drupal 全在射程内,nginx 其实另有洞。拆成因与修复顺序
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
|
12天前
|
JSON 程序员 数据格式
最新版Notepad++下载、安装、配置保姆级教程(附官网安装包)
Notepad++ 是一款免费开源、轻量快速的 Windows 文本编辑器,支持多标签、语法高亮、代码折叠与宏录制,体积小、启动快、资源占用低,兼容 x64/x32/ARM 架构,是程序员和文字工作者的高效利器。
1743 0
|
12天前
|
安全 Linux 芯片
【万字长文】Ollama下载、安装、配置、使用保姆级教程(2026最新图解)
Ollama 是一款免费开源的本地大模型运行工具,支持 DeepSeek、Qwen 等主流模型,一条命令即可下载运行。2026 最新版 v0.35.1 官方安装包(1.47GB)已提供国内直连下载,纯本地、离线可用、无广告、无隐私泄露风险。
1139 2
|
16天前
|
存储 人工智能 安全
千问办公官网入口网页版登录:qwenwork.cn 注册送2000积分、登录送100积分
阿里云千问办公(QwenWork)是AI智能工作平台,支持一句话生成PPT、表格、网页、视频及数据分析。个人免费版送2000积分+每日登录赠100分,含1GB存储、5个发布页;企业版198元/席/月起。阿里千问办公QwenWork官网:https://t.aliyun.com/U/0VCTGt 阿里AI工作平台,一句话完成数据分析、PPT 生成、视频剪辑、网页搭建等复杂任务
|
4天前
|
安全 程序员 应用服务中间件
PHP 被曝两大致命漏洞,请所有程序员抓紧做这件事!
PHP 官方发布了紧急安全更新,一口气修复了两个严重漏洞。如果网站和服务器用了 PHP 应该怎么处理?漏洞的原理是什么?本文给你讲清楚
503 0

热门文章

最新文章