Leaflet 综合案例-聚类图层控制

看过的知识不等于学会。唯有用心总结、系统记录,并通过温故知新反复实践,才能真正掌握一二

作为一名摸爬滚打三年的前端开发,开源社区给了我饭碗,我也将所学的知识体系回馈给大家,助你少走弯路!
OpenLayers、Leaflet 快速入门 ,每周保持更新 2 个案例
Cesium 快速入门,每周保持更新 4 个案例

Leaflet 综合案例-聚类图层控制

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

主要功能

MP4效果动画链接地址

技术栈

该环境下代码即拿即用

bash 复制代码
Vue 3.5.13+
Leaflet 1.9.4
Vite 6.3.5+

插件

使用 Leaflet 插件 Leaflet.markercluster 实现聚类图层

bash 复制代码
npm install leaflet.markercluster
vue 复制代码
<template>
  <div class="map-wrapper">
    <div id="map-cluster" class="map-container"></div>
  </div>
</template>

<script setup>
import { onMounted, onUnmounted } from "vue";
import "leaflet/dist/leaflet.css";
import "leaflet.markercluster/dist/MarkerCluster.css"; // 聚合插件的CSS
import "leaflet.markercluster/dist/MarkerCluster.Default.css"; // 聚合插件默认主题CSS
import L from "leaflet";
import "leaflet.markercluster"; // 引入聚合插件JS

let map = null;
let markers = null;

const initialView = [39.909186, 116.397479];
const initialZoom = 10; // 初始缩放级别稍微小一点,更容易看到聚合效果

onMounted(() => {
  map = L.map("map-cluster").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);

  // 创建一个标记点聚合图层
  markers = L.markerClusterGroup();

  // 随机生成1000个标记点
  const dummyMarkers = [];
  for (let i = 0; i < 1000; i++) {
    const lat = 39.909186 + (Math.random() - 0.5) * 0.5; // 在中心点附近随机生成
    const lng = 116.397479 + (Math.random() - 0.5) * 0.5;
    const marker = L.marker([lat, lng]).bindPopup(`标记点 ${i + 1}`);
    dummyMarkers.push(marker);
  }

  // 将所有标记点添加到聚合图层
  markers.addLayers(dummyMarkers);
  map.addLayer(markers);

  // 调整地图视图以适应所有标记点(可选,如果标记点范围很大)
  // map.fitBounds(markers.getBounds());
});

onUnmounted(() => {
  if (map) {
    map.remove();
    map = null;
    markers = 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;
  }
}

.map-container {
  flex-grow: 1;
  height: 100%;
  min-height: 300px;
  background-color: #e0e0e0;
}
</style>
相关推荐
LBuffer34 分钟前
破解入门学习笔记题三十八
笔记·学习
sensen_kiss1 小时前
INT305 Machine Learning 机器学习 Pt.5 神经网络(Neural network)
人工智能·神经网络·机器学习
阿猿收手吧!1 小时前
【成长经历】秋招经历/技术之路分享
经验分享
CodeCraft Studio1 小时前
PPT处理控件Aspose.Slides教程:使用Java将PowerPoint笔记导出为PDF
java·笔记·pdf·powerpoint·aspose·ppt转pdf·java将ppt导出pdf
仰望—星空2 小时前
MiniEngine学习笔记 : DescriptorHeap
windows·笔记·学习
Victory_orsh3 小时前
“自然搞懂”深度学习(基于Pytorch架构)——010203
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
长桥夜波3 小时前
机器学习日报10
人工智能·机器学习
go_bai3 小时前
Linux--进程池
linux·c++·经验分享·笔记·学习方法
QT 小鲜肉3 小时前
【QT/C++】Qt网络编程进阶:UDP通信和HTTP请求的基本原理和实际应用(超详细)
c语言·网络·c++·笔记·qt·http·udp
轻舟客丶4 小时前
ORA-03113的解决方案
数据库·经验分享·笔记·oracle