当前位置: 首页 > news >正文

【WebGIS】Vue3使用 VueLeaflet + 天地图 搭建地图可视化平台(基础用法)

初始化

创建项目

  • nodejs 18.0.6
  • npm 9.5.1
    在这里插入图片描述

引入地图服务

VueLeaflet

GitHub - vue-leaflet/vue-leaflet: vue-leaflet 与 vue3 兼容

Vue Leaflet (vue2-leaflet)

  1. package.josn安装版本

直接添加四个依赖

{// ..."scripts": {// ...},"dependencies": {"leaflet-velocity": "^2.1.4","ts-debounce": "^4.0.0",// ...},"devDependencies": {"@types/leaflet": "^1.9.0","leaflet": "^1.9.4",// ... }
}
  1. 直接在 src 目录下引入 VueLeaflt 包本地 VueLeaflt组件压缩包
  2. 新建 map 地图组件使用 VueLeaflet 地图框架
<template><div id="map"><div class="map-container"><!-- 地图组件 --><div class="map"><l-mapref="map"v-model:zoom="zoom":center="mapCenter":attribution-control="false":zoom-control="false"><l-layer-group v-for="(item, index) in baseLayers" :key="index"><!-- 地图底图 --><l-layer-group><l-tile-layerv-if="item.show":url="item.url"layer-type="base":name="item.name"></l-tile-layer></l-layer-group></l-layer-group></l-map></div></div></div></template>

JavaScript 引入

<script setup lang="ts">
import { ref, onUnmounted } from 'vue'
import { LMap, LLayerGroup, LTileLayer } from '@/VueLeaflet/components/index'
import 'leaflet/dist/leaflet.css'</srcipt>
  1. 切记使用标签时需要先引入
import {LMap,LLayerGroup,LTileLayer,// ...
} from '@/VueLeaflet/components/index';

天地图

天地图 开发管理平台 (tianditu.gov.cn)

天地图API (tianditu.gov.cn)

创建应用获取 key 密钥, 绘制地图地图

// 获取env密钥
const TIANDITU_KEY = import.meta.env.VITE_TIANDITU_KEY;// 地图底图
const baseLayers = ref([{index: 1,name: '行政图',url: `https://t0.tianditu.gov.cn/DataServer?T=vec_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,// url: '/map/{z}/{x}/{y}.png',// 矢量注记anno: `https://t0.tianditu.gov.cn/DataServer?T=cva_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,show: true,},{index: 2,name: '影像图',url: `https://t0.tianditu.gov.cn/DataServer?T=img_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,anno: `https://t0.tianditu.gov.cn/DataServer?T=cia_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,show: false,},{index: 3,name: '地形图',url: `https://t0.tianditu.gov.cn/DataServer?T=ter_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,anno: `https://t0.tianditu.gov.cn/DataServer?T=cta_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,show: false,},
])

结合组件

创建地图组件map 使用VueLeaflet嵌套天地图服务

供其他页面使用

<template><div id="map"><div class="map-container"><!-- 地图组件 --><div class="map"><l-mapref="map"v-model:zoom="zoom":center="mapCenter":attribution-control="false":zoom-control="false"><l-layer-group v-for="(item, index) in baseLayers" :key="index"><!-- 地图底图 --><l-layer-group><l-tile-layerv-if="item.show":url="item.url"layer-type="base":name="item.name"></l-tile-layer><!--   省市标注图层    --><l-tile-layerv-if="item.anno && item.show":url="item.anno"layer-type="annotation":name="item.name + '标注'"></l-tile-layer></l-layer-group></l-layer-group></l-map></div></div></div></template><script setup lang="ts">
import { ref, onUnmounted } from 'vue'
import { LMap, LLayerGroup, LTileLayer } from '@/VueLeaflet/components/index'
import 'leaflet/dist/leaflet.css'const map = ref()
const mapCenter = ref([24.611, 113.5387])
const zoom = ref(9)const TIANDITU_KEY = import.meta.env.VITE_TIANDITU_KEY;// 地图底图
const baseLayers = ref([{index: 1,name: '行政图',url: `https://t0.tianditu.gov.cn/DataServer?T=vec_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,// url: '/map/{z}/{x}/{y}.png',// 矢量注记anno: `https://t0.tianditu.gov.cn/DataServer?T=cva_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,show: true,},{index: 2,name: '影像图',url: `https://t0.tianditu.gov.cn/DataServer?T=img_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,anno: `https://t0.tianditu.gov.cn/DataServer?T=cia_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,show: false,},{index: 3,name: '地形图',url: `https://t0.tianditu.gov.cn/DataServer?T=ter_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,anno: `https://t0.tianditu.gov.cn/DataServer?T=cta_w&X={x}&Y={y}&L={z}&tk=${TIANDITU_KEY}`,show: false,},
])</script><style lang="scss" scoped>
#map {
}.map-container {position: relative;height: 100vh;width: 100vw;overflow: hidden;
}.map {height: 100%;width: 100%;
}</style>

地图API处理

marker叠加标记点

基础叠加: 叠加marker图片

示例叠加 雷电图标 自动站图标, 提供 宽度、高度、经度、纬度、图标路径

        <!--   marker图层    --><l-layer-group><l-markerv-for="(marker, markerIndex) in markers":key="markerIndex":lat-lng="[marker.latitude, marker.longitude]"><!--  展示雷达 自动站 marker   --><l-ico:icon-size="[marker.width, marker.height]":icon-anchor="[18, 18]":class-name="'my-div-icon'":icon-url="marker.iconPath"/></l-marker></l-layer-group>

pinia全局管理 雷电数据内容

  const getAllThunderData = () => {const width = 20;const height = 20;thunderData.value = thunderData.value.map((point, index) => ({...point,id: index + 1, // 累加idwidth,height: point.intensity < 0 ? height / 5 : height, // 负号是长方形iconPath:point.intensity >= 0? '/src/assets/images/icon/windArrow/positive.png' // intensity雷暴强度 >= 0: '/src/assets/images/icon/windArrow/negative.png',type: 'thunder',}));return thunderData.value;};

基础叠加显示数值,不显示图标

示例展示 降水数据,直接将降水数值叠加在地图上

也是使用 l-icon 标签,只不过不添加 :icon-url 字段

直接在icon里面使用 div 展示数据即可

        <!--   marker图层    --><l-layer-group><l-markerv-for="(marker, markerIndex) in markers":key="markerIndex":lat-lng="[marker.latitude, marker.longitude]"><!-- 显示降水数值 --><l-iconv-if="marker.type === 'rain'":icon-size="[20, 28]":icon-anchor="[28, 28]":class-name="'my-div-icon'"><div class="rain-marker">{{ marker.rainfall }}</div></l-icon></l-marker></l-layer-group>

绘制区域轮廓

DataV.GeoAtlas地理小工具系列 (aliyun.com)

访问网址, 下载所需区域的GeoJSON数据并保存, 最细颗粒度为 区

  1. 读取 assets 下的 json 数据
import {LGeoJson,
} from '@/VueLeaflet/components/index';
import type { GeoJSON } from 'geojson';// GeoJSON 数据
const geoJsonData = ref<GeoJSON | null>(null);// 样式
const geoJsonOptions = ref({style: {color: '#3388ff',weight: 2,opacity: 1, // 边界透明度fillColor: '#3388ff',fillOpacity: 0.2 // 填充透明度}
});// 加载  GeoJSON 数据
const loadGeoJsonData = async () => {try {const response = await fetch('/src/assets/json/xxxx.json');const data = await response.json();geoJsonData.value = data;} catch (error) {console.error('加载 GeoJSON 数据失败:', error);}
};onMounted(() => {loadGeoJsonData();
});
  1. 在地图上进行绘制
<l-layer-group v-for="(item, index) in baseLayers" :key="index"><!--  GeoJSON 图层轮廓 --><l-layer-group><l-geo-jsonv-if="geoJsonData":geojson="geoJsonData":options="geoJsonOptions"/></l-layer-group>// ......

文章转载自:

http://jINCohYW.mbnhr.cn
http://LQlpFh99.mbnhr.cn
http://9RCsiFtX.mbnhr.cn
http://EDTwwopC.mbnhr.cn
http://hEffj35F.mbnhr.cn
http://96VhXkDV.mbnhr.cn
http://fyBdftNK.mbnhr.cn
http://wca1oPU6.mbnhr.cn
http://n5QMY8vQ.mbnhr.cn
http://9zh32stQ.mbnhr.cn
http://dB4ZlzUx.mbnhr.cn
http://JJjeE1V4.mbnhr.cn
http://U4pUlkIJ.mbnhr.cn
http://d7LqFVbm.mbnhr.cn
http://tDJ5fQ4j.mbnhr.cn
http://jZ0jOt88.mbnhr.cn
http://d58hGfxN.mbnhr.cn
http://pSBrJrC9.mbnhr.cn
http://Xd0towyE.mbnhr.cn
http://Fm5DfUWv.mbnhr.cn
http://YMR2QOX2.mbnhr.cn
http://hHW9WJ8D.mbnhr.cn
http://CwO1BuE1.mbnhr.cn
http://CpZ49mXO.mbnhr.cn
http://50dYLEuw.mbnhr.cn
http://dWnpcGQZ.mbnhr.cn
http://f7lXXMS7.mbnhr.cn
http://ZcsBF2Jy.mbnhr.cn
http://AOymyb4R.mbnhr.cn
http://V0XhKcbm.mbnhr.cn
http://www.dtcms.com/a/385609.html

相关文章:

  • 69-SQLite应用
  • Day06 双指针扫描 | 11. 盛最多水的容器
  • LeetCode 刷题【77. 组合、78. 子集、79. 单词搜索】
  • Jenkins 构建清理策略:自带功能 vs Discard Old Build 插件,全场景实操指南
  • DevOps历程-Gogs的安装与部署
  • FreeRTOS 任务静态创建与句柄详解
  • 嵌入式音视频开发——RTMP协议详解
  • 每日一题(6)
  • 信号量主要API及综合应用
  • 【开题答辩全过程】以 B站用户视频喜好倾向数据分析系统为例,包含答辩的问题和答案
  • ARM架构学习6.2——中断理解
  • 搭建Qt5.14.2+msvc2017_x64项目测试Opencv4.10功能
  • Steger 算法 的原理和流程
  • WD5030K:一款7-30V宽输入范围、12A高效同步降压DC-DC转换器芯片详解
  • 《2025年AI产业发展十大趋势报告》五十七
  • 滴滴试点返程费自主议价将会怎么改变市场?
  • 【嵌入式原理系列-第八篇】USART从原理到配置全解析
  • Python4-seaborn
  • 使用 Aamzon Step Functions 重构无服务器工作流
  • 模电基础:场效应管
  • Typescript工具类型
  • Spring异步编程- 浅谈 Reactor 核心操作符
  • 21.5 单卡24G训7B大模型!HuggingFace TRL+QLoRA实战,3倍提速显存直降70%
  • git中,如果在文件夹A下有文件夹B、C文件夹,现在在A下创建仓库,连接远程仓库,那么如何在提交的时候忽略B、C,排除对B、C管理
  • Java Web 入门实战:SpringBoot+Spring MVC 从 0 到 1 学习指南
  • 电磁流量计可靠品牌之选,基恩士提供多样化解决方案
  • 三大基础无源电子元件——电阻(R)、电感(L)、电容(C)
  • Baklib:从传统到AI驱动的新一代数字体验平台
  • 机器视觉在人形机器人中有哪些检测应用
  • Java的Arrays类