SuperMap iServer + vue3 实现点聚合 超简单!

先安装依赖

java 复制代码
npm install leaflet.markercluster --save
npm install patch-package --save-dev  
npm install @supermap/iclient-leaflet --save
npm install @supermap/iclient-vue  

直接cv就可以

javascript 复制代码
<template>
  <!-- 地图容器,必须设置宽高 -->
  <div id="map" style="width: 100vw; height: 100vh;"></div>
</template>

<script setup>
import { onMounted } from 'vue';
// 1. 引入核心依赖(只保留必要的)
import L from 'leaflet';
import 'leaflet.markercluster'; // 通用聚合插件
import '@supermap/iclient-leaflet'; // 仅用于加载SuperMap底图

let map = null;

// 2. 生成测试用的点数据(标准GeoJSON,无需转换)
const getTestPointData = () => {
  const features = [];
  // 生成100个北京周边的随机点
  for (let i = 0; i < 100; i++) {
    const lng = 116.39 + (Math.random() - 0.5) * 0.5; // 经度范围
    const lat = 39.9 + (Math.random() - 0.5) * 0.5;   // 纬度范围
    features.push({
      type: 'Feature',
      geometry: { type: 'Point', coordinates: [lng, lat] },
      properties: { name: `点位${i + 1}`, id: i }
    });
  }
  return { type: 'FeatureCollection', features };
};

// 3. 初始化地图和聚合
const initMap = () => {
  // 初始化地图(坐标系匹配SuperMap底图)
  map = L.map('map', {
    crs: L.CRS.EPSG3857, // Web墨卡托,必须和底图一致
    center: [39.9, 116.39], // 北京中心点
    zoom: 12, // 初始缩放级别
    zoomControl: true
  });

  // 加载SuperMap底图(确保地址正确)
  L.supermap.tiledMapLayer(
      'http://support.supermap.com.cn:8090/iserver/services/map-china400/rest/maps/China',
      { noWrap: true }
  ).addTo(map);
  // L.supermap.tiledMapLayer(
  //     'https://iserver.supermap.io/iserver/services/map-china400/rest/maps/China', // 服务根地址(不带tileImage)
  //     {
  //       noWrap: true,
  //       tileSize: 256 // 瓦片尺寸与服务一致
  //     }
  // ).addTo(map);
  // 4. 创建聚合组(核心:通用聚合方案,无SuperMap依赖)
  const clusterGroup = L.markerClusterGroup({
    distance: 50, // 聚合距离(像素),值越小聚合越精细
    minClusterSize: 2, // 最少2个点才聚合
    maxZoom: 18, // 缩放超过18级则取消聚合
    // 自定义聚合点样式(数字气泡)
    iconCreateFunction: (cluster) => {
      const count = cluster.getChildCount(); // 获取聚合内的点数
      // 动态调整颜色和大小
      const color = count > 10 ? '#ff0000' : count > 5 ? '#ff9900' : '#0099ff';
      const size = 20 + Math.min(count / 10, 10); // 最大不超过30px

      return L.divIcon({
        html: `<div style="
          width: ${size}px; height: ${size}px;
          border-radius: 50%; background: ${color};
          color: white; text-align: center; line-height: ${size}px;
          font-size: 12px; font-weight: bold;
        ">${count}</div>`,
        className: 'custom-cluster',
        iconSize: [size, size]
      });
    }
  });

  // 5. 处理点数据并加入聚合组
  const pointData = getTestPointData();
  L.geoJSON(pointData, {
    // 自定义单个点的样式(圆形标记)
    pointToLayer: (feature, latlng) => {
      return L.circleMarker(latlng, {
        radius: 6,
        fillColor: '#0099ff',
        fillOpacity: 0.8,
        stroke: true,
        strokeColor: '#fff',
        strokeWidth: 1
      }).bindPopup(`<div>${feature.properties.name}</div>`); // 单个点弹窗
    }
  }).eachLayer(layer => clusterGroup.addLayer(layer));

  // 6. 将聚合组添加到地图
  map.addLayer(clusterGroup);

  // 可选:聚合点点击事件(点击放大)
  clusterGroup.on('click', (e) => {
    if (e.layer.getChildCount) { // 判断是否是聚合点
      map.flyTo(e.layer.getLatLng(), map.getZoom() + 1);
    }
  });
};

// 组件挂载后初始化
onMounted(() => {
  initMap();
});
</script>

<style scoped>
/* 必须引入所有样式,否则地图/聚合点样式错乱 */
@import 'leaflet/dist/leaflet.css';
@import 'leaflet.markercluster/dist/MarkerCluster.css';
@import 'leaflet.markercluster/dist/MarkerCluster.Default.css';
@import '@supermap/iclient-leaflet/dist/iclient-leaflet.css';

/* 修复聚合点样式穿透(Vue3 scoped 必须加:deep) */
:deep(.custom-cluster) {
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
}

/* 确保地图容器层级正常 */
#map {
  z-index: 1;
}
</style>
相关推荐
css趣多多7 小时前
this.$watch
前端·javascript·vue.js
Code小翊7 小时前
JS语法速查手册,一遍过JS
javascript
子春一7 小时前
Flutter for OpenHarmony:构建一个 Flutter 天气卡片组件,深入解析动态 UI、响应式布局与语义化设计
javascript·flutter·ui
有来技术7 小时前
ASP.NET Core 权限管理系统(RBAC)设计与实现|vue3-element-admin .NET 后端
vue.js·后端·c#·asp.net·.net
qq_12498707537 小时前
基于springboot的林业资源管理系统设计与实现(源码+论文+部署+安装)
java·vue.js·spring boot·后端·spring·毕业设计·计算机毕业设计
Remember_9937 小时前
Java 单例模式深度解析:设计原理、实现范式与企业级应用场景
java·开发语言·javascript·单例模式·ecmascript
写代码的【黑咖啡】7 小时前
Python 中的自然语言处理利器:NLTK
前端·javascript·easyui
qq_12498707538 小时前
基于springboot的竞赛团队组建与管理系统的设计与实现(源码+论文+部署+安装)
java·vue.js·spring boot·后端·信息可视化·毕业设计·计算机毕业设计
可问春风_ren8 小时前
Vue3 入门详解:从基础到实战
开发语言·前端·javascript·vue.js·前端框架·ecmascript·edge浏览器
摘星编程8 小时前
用React Native开发OpenHarmony应用:NativeStack原生导航
javascript·react native·react.js