ArcGIS JS 基础教程(2):地图切换底图(天地图)

ArcGIS JS 基础教程(2):地图切换底图(天地图)

零、写在前面

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

💡 在线示例 :完整可运行的 HTML 示例,无需任何环境配置,可直接在浏览器中打开体验

🗂️ 专栏导航 :收藏 + 关注,专栏文章第一时间送达

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


一、功能介绍

实现天地图底图(街道图、卫星图、地形图标)之间的切换,同时支持加载自定义底图(如本地瓦片底图、第三方底图服务),满足不同项目的可视化需求。

二、功能实现

利用Map的basemap属性切换官方底图;自定义底图通过TileLayer加载瓦片服务,配置url、opacity等参数,添加到地图实例中实现切换。

三、功能应用

地图可视化项目中,用户根据需求切换不同底图(如卫星图查看实际地形、街道图查看道路分布),如国土测绘、城市规划类项目。

四、核心代码

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>第2课:地图切换底图</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.9);
            padding: 8px 20px;
            border-radius: 4px;
            font-size: 18px;
            font-weight: bold;
            z-index: 100;
        }
        .toolbar {
            position: absolute;
            top: 70px;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(255,255,255,0.95);
            padding: 10px 20px;
            border-radius: 8px;
            display: flex;
            gap: 10px;
            z-index: 100;
            box-shadow: 0 2px 8px rgba(0,0,0,0.15);
        }
        .toolbar button {
            padding: 6px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s;
        }
        .btn-vec { background: #4CAF50; color: white; }
        .btn-img { background: #2196F3; color: white; }
        .btn-ter { background: #FF9800; color: white; }
        .btn-sdt { background: #9C27B0; color: white; }
        .toolbar button:hover { opacity: 0.85; transform: scale(1.05); }
        .info {
            position: absolute;
            bottom: 20px;
            left: 20px;
            background: rgba(255,255,255,0.9);
            padding: 10px 16px;
            border-radius: 6px;
            font-size: 13px;
            z-index: 100;
            max-width: 300px;
        }
    </style>
</head>
<body>
<h1 class="page-title">第2课:地图切换底图</h1>
<div class="toolbar">
    <button class="btn-vec" id="switchVec">矢量底图</button>
    <button class="btn-img" id="switchImg">影像底图</button>
    <button class="btn-ter" id="switchTer">地形底图</button>
    <button class="btn-sdt" id="switchSdt">自定义天地图</button>
</div>
<div id="mapContainer"></div>
<div class="info" id="info">当前底图:<span id="currentBasemap">矢量底图</span></div>

<script type="module">
    const Map = await $arcgis.import("@arcgis/core/Map.js");
    const SceneView = await $arcgis.import("@arcgis/core/views/SceneView.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] },
        ground: {
            surface: {
                elevationLayers: [{
                    url: "https://www.geosceneonline.cn/image/rest/services/OpenData/ChinaTerrain3D/ImageServer/"
                }]
            }
        }
    });

    const view = new SceneView({
        container: "mapContainer",
        map: map,
        center: [118.805, 34.027],
        zoom: 13,
        tilt: 45
    });

    // 底图切换函数
    const switchBasemap = (type, label) => {
        const layers = getTianditu.default({ type });
        map.basemap = { baseLayers: [layers.base, layers.anno] };
        document.getElementById("currentBasemap").textContent = label;
    };

    document.getElementById("switchVec").onclick = () => switchBasemap("vec_w", "矢量底图");
    document.getElementById("switchImg").onclick = () => switchBasemap("img_w", "影像底图");
    document.getElementById("switchTer").onclick = () => switchBasemap("ter_w", "地形底图");

    view.when(() => console.log("地图切换底图示例加载完成!"));
</script>
</body>
</html>

五、在线示例

🔗 在线体验地址(GitHub资源,等待时间较长,或者架梯子)https://southjor.github.io/arcgis-examples/lessons/lesson2.html

六、底图类型速查

type 参数 说明 应用场景
vec_w 矢量底图 默认导航、街道路网展示
img_w 影像底图 卫星图、地形查看
ter_w 地形底图 山体走势、高程可视化

七、系列导航

⬅️ 上一篇ArcGIS JS 基础教程(1):地图初始化(含AMD/ESM两种引入方式)

相关推荐
城数派17 小时前
2025年全国地级市间驾车出行距离和出行时间矩阵数据
数据库·arcgis·信息可视化·数据分析
你的不安1 天前
ArcGIS中文包安装了,为什么ArcGIS Administrator没有识别到
arcgis·中文包
GIS地信小匠1 天前
(29)ArcGIS Pro 添加XY坐标与XY转线:坐标数据转矢量工具实操
arcgis
城数派2 天前
2025年南京市全类别POI(55W+数据)
数据库·arcgis·信息可视化·数据分析·excel
城数派3 天前
2000-2025年我国省市县三级逐8天日间地表温度数据(Shp/Excel格式)
数据库·arcgis·信息可视化·数据分析·excel
GIS地信小匠3 天前
(28)ArcGIS Pro 要素包络矩形转面与最小边界几何:边界提取双工具全攻略
arcgis·空间分析·gis制图·边界提取·空间数据处理·gis教程·arcgls pro
无心使然云中漫步3 天前
ArcGis常用服务介绍及Arcgis,Openlayers,Leaflet加载
开发语言·arcgis·php
非科班Java出身GISer4 天前
ArcGIS JS 基础教程(1):地图初始化(含AMD/ESM两种引入方式)
javascript·arcgis·arcgis js·arcgis js 初始化·arcgis js 地图初始化
智航GIS4 天前
ArcGIS 启动报错?两种方法快速解决许可启动失败问题
arcgis