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

Leaflet 综合案例-矢量图层控制

看过的知识不等于学会。唯有用心总结、系统记录,并通过温故知新反复实践,才能真正掌握一二
作为一名摸爬滚打三年的前端开发,开源社区给了我饭碗,我也将所学的知识体系回馈给大家,助你少走弯路!
OpenLayers、Leaflet 快速入门 ,每周保持更新 2 个案例
Cesium 快速入门,每周保持更新 4 个案例

Leaflet 综合案例-矢量图层控制

Vue 3 + Leaflet 实现的 WebGIS 应用提供了完整的矢量图层控制功能

主要功能

  1. 切换不同类型的地图元素(标记点、图形、GeoJSON)

在这里插入图片描述

MP4效果动画链接地址

技术栈

该环境下代码即拿即用

Vue 3.5.13+
Leaflet 1.9.4
Vite 6.3.5+
<template><div class="map-wrapper"><div class="control-panel"><h2>地图图层控制</h2><div class="control-group"><label><input type="checkbox" v-model="showMarkers" />显示标记点</label><label><input type="checkbox" v-model="showShapes" />显示绘制图形</label><label><input type="checkbox" v-model="showGeoJSON" />显示 GeoJSON 数据</label></div><button @click="resetMapView" class="reset-button">重置地图视图</button></div><div id="comprehensive-map" class="map-container"></div></div>
</template><script setup>
import { ref, onMounted, onUnmounted, watch } from "vue";
import "leaflet/dist/leaflet.css";
import L from "leaflet";let map = null;
let markerLayerGroup = null;
let shapeLayerGroup = null;
let geojsonLayerGroup = null;const showMarkers = ref(true);
const showShapes = ref(true);
const showGeoJSON = ref(true);// 自定义图标定义
const customIcon = L.icon({iconUrl: "/src/assets/car.png",iconSize: [38, 38],iconAnchor: [19, 38],popupAnchor: [0, -38],
});// 示例 GeoJSON 数据
const geojsonData = {type: "FeatureCollection",features: [{type: "Feature",properties: {name: "北京故宫",popupContent: "这是故宫博物院,中国明清两代的皇家宫殿。",},geometry: {type: "Point",coordinates: [116.397972, 39.916042],},},{type: "Feature",properties: {name: "北京二环路",style: {color: "#ff7800",weight: 5,opacity: 0.65,},},geometry: {type: "LineString",coordinates: [[39.92, 116.36],[39.93, 116.38],[39.92, 116.4],[39.9, 116.42],],},},{type: "Feature",properties: {name: "北京公园",style: {color: "#0000ff",weight: 2,opacity: 0.8,fillColor: "#00ff00",fillOpacity: 0.5,},},geometry: {type: "Polygon",coordinates: [[[116.45, 39.9],[116.47, 39.9],[116.47, 39.88],[116.45, 39.88],[116.45, 39.9],],],},},],
};const initialView = [39.909186, 116.397479];
const initialZoom = 12;onMounted(() => {// 初始化地图map = L.map("comprehensive-map").setView(initialView, initialZoom);// 添加高德地图瓦片图层L.tileLayer("https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}",{maxZoom: 18,minZoom: 3,attribution: '&copy; <a href="https://www.amap.com/">高德地图</a>',}).addTo(map);// 初始化图层组markerLayerGroup = L.layerGroup().addTo(map);shapeLayerGroup = L.layerGroup().addTo(map);geojsonLayerGroup = L.layerGroup().addTo(map);// 添加标记点L.marker([39.909186, 116.387479], { icon: customIcon }).bindPopup("<b>自定义图标标记</b>").addTo(markerLayerGroup);L.marker([39.92, 116.42]).bindPopup("<b>另一个标记点</b>").addTo(markerLayerGroup);// 添加折线const latlngsPolyline = [[39.9, 116.35],[39.95, 116.4],[39.9, 116.45],];L.polyline(latlngsPolyline, { color: "red" }).addTo(shapeLayerGroup);// 添加多边形const latlngsPolygon = [[39.88, 116.38],[39.88, 116.42],[39.85, 116.42],[39.85, 116.38],];L.polygon(latlngsPolygon, {color: "blue",fillColor: "#f03",fillOpacity: 0.5,}).addTo(shapeLayerGroup);// 添加 GeoJSON 数据L.geoJSON(geojsonData, {onEachFeature: function (feature, layer) {if (feature.properties && feature.properties.popupContent) {layer.bindPopup(feature.properties.popupContent);}},pointToLayer: function (feature, latlng) {return L.circleMarker(latlng, {radius: 8,fillColor: "#ff7800",color: "#000",weight: 1,opacity: 1,fillOpacity: 0.8,});},style: function (feature) {return feature.properties && feature.properties.style? feature.properties.style: {};},}).addTo(geojsonLayerGroup);// 监听图层显示状态的变化watch(showMarkers, (newValue) => {if (newValue) {map.addLayer(markerLayerGroup);} else {map.removeLayer(markerLayerGroup);}});watch(showShapes, (newValue) => {if (newValue) {map.addLayer(shapeLayerGroup);} else {map.removeLayer(shapeLayerGroup);}});watch(showGeoJSON, (newValue) => {if (newValue) {map.addLayer(geojsonLayerGroup);} else {map.removeLayer(geojsonLayerGroup);}});
});onUnmounted(() => {if (map) {map.remove(); // 组件卸载时销毁地图实例map = null;}
});const resetMapView = () => {if (map) {map.setView(initialView, initialZoom);}
};
</script><style scoped>
.map-wrapper {display: flex;flex-direction: column; /* 默认垂直布局,小屏幕下控制面板在地图上方 */height: 100vh; /* 占据整个视口高度 */width: 100vw;font-family: sans-serif;box-sizing: border-box; /* 确保内边距和边框包含在宽度和高度内 */
}@media (min-width: 768px) {.map-wrapper {flex-direction: row; /* 大屏幕下水平布局,控制面板在左侧 */}
}.control-panel {flex-shrink: 0; /* 不压缩 */width: 100%; /* 小屏幕下占满宽度 */background-color: #f8f8f8;border-right: 1px solid #eee;box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);display: flex;flex-direction: column;gap: 15px;
}@media (min-width: 768px) {.control-panel {width: 250px; /* 大屏幕下固定宽度 */height: 100%; /* 占据整个高度 */}
}.control-panel h2 {margin-top: 0;margin-bottom: 15px;color: #333;font-size: 1.2em;
}.control-group {display: flex;flex-direction: column;gap: 10px;
}.control-group label {display: flex;align-items: center;cursor: pointer;color: #555;
}.control-group input[type="checkbox"] {margin-right: 8px;transform: scale(1.2); /* 放大复选框 */
}.map-container {flex-grow: 1; /* 占据剩余空间 */height: 100%; /* 确保地图容器在flex布局中占据高度 */min-height: 300px; /* 最小高度,防止在小屏幕下地图过小 */background-color: #e0e0e0; /* 地图加载前的背景色 */
}.reset-button {padding: 10px 15px;background-color: #007bff;color: white;border: none;border-radius: 5px;cursor: pointer;font-size: 1em;transition: background-color 0.3s ease;margin-top: auto; /* 将按钮推到底部 */
}.reset-button:hover {background-color: #0056b3;
}.reset-button:active {background-color: #004085;
}
</style>
http://www.dtcms.com/a/303476.html

相关文章:

  • 二分查找的「左右为难」:如何优雅地找到数组中元素的首尾位置
  • SQL笔试面试
  • 深入理解 Qt 信号与槽机制的底层逻辑
  • AUTOSAR Mcal SPI - EB工具配置介绍
  • Android Handler 完全指南
  • 手游遇攻击为何要用游戏盾SDK?
  • Linux学习--C语言(指针3)
  • 第三阶段—8天Python从入门到精通【itheima】-139节(pysqark实战-前言介绍)
  • linux du、df命令使用教程
  • AWS Bedrock Claude 3 API的完整指南
  • 基于STM32设计的智慧果园云监测系统_256
  • 从像素到频率:OpenCV傅里叶变换
  • 扑克洗牌
  • NVMe高速传输之摆脱XDMA设计18:PRP控制模块设计
  • NVMe高速传输之摆脱XDMA设计21:PCIe请求模块设计(下)
  • 机器学习基础-matplotlib
  • clock_nanosleep系统调用及示例
  • node后端-JWT认证
  • Excel VBA宏的使用
  • 大模型应用班-第3课 从Excel到大屏:AI编程实战全解析 HW3 从零到一:香港疫情数据看板开发实战指南
  • 【GoLang #4】:Go 语言 函数详述(定义调用 | 匿名函数 闭包 | defer)
  • windows clion远程连接ubuntu运行调试nginx-1.22.1版本
  • 【优先级高,先补充】基于文本增强跨模态特征交互注意网络的多模态情感分析
  • SVN与GIT的区别,分别使用与哪些管理场景?
  • 《汇编语言:基于X86处理器》第10章 结构和宏(2)
  • Linux——线程池的模拟实现
  • 解决c++静态成员编译报错:‘xxx‘ is not a member of ‘xxx‘ 问题
  • 第五届先进算法与神经网络国际学术会议(AANN 2025)
  • vue项目进首页不加载全部资源
  • 【数据结构初阶】--二叉树(三)