openlayers关于clusters介绍

效果展示

初始化地图

Javascript 复制代码
<template>
  <div id="map" class="map"></div>
</template>

<script>
import Map from "ol/Map.js";
import View from "ol/View.js";
import { centerPoint } from "@/utils/showPage.js";
import { gaodeMapLayer } from "@/utils/MapLayer.js";
import { clusters, clusterCircleStyle, clusterCircles } from "@/utils/cluster";
import { createEmpty, extend, getHeight, getWidth } from "ol/extent.js";
export default {
  name: "cluster",
  data() {
    return {
      map: null
    };
  },
  mounted() {
    const map = new Map({
      layers: [gaodeMapLayer, clusters, clusterCircles],
      target: "map",
      view: new View({
        center: centerPoint,
        zoom: 11,
        minZoom: 2,
        maxZoom: 30,
        projection: "EPSG:4326",
        rotation: 0,
      }),
    });
  },
};
</script>

<style scoped>
.map {
  width: 100%;
  height: 900px;
}
</style>

图层详解

  • 公共方法和样式

    Javascript 复制代码
     function gasPointType() {
       let customPoint = config.CUSTOM_POINT.val,
         GAS_POINT_TYPE = config.GAS_POINT_TYPE.children;
       GAS_POINT_TYPE.forEach((item) => {
         item.type = item.val.typeId.value;
       });
       return [
         ...GAS_POINT_TYPE,
         { type: customPoint.typeId.value, val: customPoint },
       ];
     }
     function getICon(properties) {
       let typeId = properties.typeId || "1",
         allPoint = gasPointType();
       return allPoint.find((i) => i.type == typeId).val;
     }
     function clusterMemberStyle(clusterMember) {
       let properties = clusterMember.getProperties();
       let item = getICon(properties);
       return new Style({
         geometry: clusterMember.getGeometry(),
         image: new Icon({
           rotation: 0,
           src: item.iconUrl.value,
           anchorXUnits: "fraction",
           imgSize: [item.width.value, item.height.value],
         }),
       });
     }
     function generatePointsCircle(count, clusterCenter, resolution) {
       const circumference =
         circleDistanceMultiplier * circleFootSeparation * (2 + count);
       let legLength = circumference / (Math.PI * 2); 
       const angleStep = (Math.PI * 2) / count;
       const res = [];
       let angle;
       legLength = Math.max(legLength, 35) * resolution;
       for (let i = 0; i < count; ++i) {
    
         angle = circleStartAngle + i * angleStep;
         res.push([
           clusterCenter[0] + legLength * Math.cos(angle),
           clusterCenter[1] + legLength * Math.sin(angle),
         ]);
       }
    
       return res;
     }
  • clusters图层

    Javascript 复制代码
      const vectorSource = new VectorSource({
        features: new GeoJSON().readFeatures({
          type: devList.type,
          features: _devList,
        }),
      });
    
      const clusterSource = new Cluster({
        distance: 35,
        source: vectorSource,
      });
      }
      function clusterStyle(feature) {
        const size = feature.get("features").length;
        if (size > 1) {
          return [
            new Style({
              image: outerCircle,
            }),
            new Style({
              image: innerCircle,
              text: new Text({
                text: size.toString(),
                fill: textFill,
                stroke: textStroke,
              }),
            }),
          ];
        }
        const originalFeature = feature.get("features")[0];
        return clusterMemberStyle(originalFeature);
      }
    
      const clusters = new VectorLayer({
        source: clusterSource,
        style: clusterStyle,
      });
  • clusterCircles图层

    Javascript 复制代码
       const clusterCircles = new VectorLayer({
         source: clusterSource,
         style: clusterCircleStyle,
       });
    
       function clusterCircleStyle(
         cluster,
         resolution,
         clickFeature,
         clickResolution
       ) {
         if (cluster !== clickFeature || resolution !== clickResolution) {
           return null;
         }
         const clusterMembers = cluster.get("features");
         const centerCoordinates = cluster.getGeometry().getCoordinates();
         return generatePointsCircle(
           clusterMembers.length,
           cluster.getGeometry().getCoordinates(),
           resolution
         ).reduce((styles, coordinates, i) => {
           const point = new Point(coordinates);
           const line = new LineString([centerCoordinates, coordinates]);
           styles.unshift(
             new Style({
               geometry: line,
               stroke: convexHullStroke,
             })
           );
           styles.push(
             clusterMemberStyle(
               new Feature({
                 ...clusterMembers[i].getProperties(),
                 geometry: point,
               })
             )
           );
           return styles;
         }, []);
       }

地图pointermove和click

Javascript 复制代码
  map.on("pointermove", (event) => {
      clusters.getFeatures(event.pixel).then((features) => {
        map.getTargetElement().style.cursor =
          features[0] && features[0].get("features").length > 1
            ? "pointer"
            : "";
      });
    });

    map.on("click", (event) => {
      clusters.getFeatures(event.pixel).then((features) => {
        if (features.length > 0) {
          const clusterMembers = features[0].get("features");
          if (clusterMembers.length > 1) {
            // Calculate the extent of the cluster members.
            const extent = createEmpty();
            clusterMembers.forEach((feature) =>
              extend(extent, feature.getGeometry().getExtent())
            );
            const view = map.getView();
            const resolution = map.getView().getResolution();
            if (
              view.getZoom() === view.getMaxZoom() ||
              (getWidth(extent) < resolution && getHeight(extent) < resolution)
            ) {
              // Show an expanded view of the cluster members.
              let clickFeature = features[0];
              let clickResolution = resolution;
              clusterCircles.setStyle((cluster, resolution) => {
                clusterCircleStyle(
                  cluster,
                  resolution,
                  clickFeature,
                  clickResolution
                );
              });
            } else {
              // Zoom to the extent of the cluster members.
              view.fit(extent, { duration: 500, padding: [50, 50, 50, 50] });
            }
          }
        }
      });
    });
相关推荐
passerby606117 分钟前
完成前端时间处理的另一块版图
前端·github·web components
掘了25 分钟前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅28 分钟前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅1 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅1 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment1 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅2 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊2 小时前
jwt介绍
前端
爱敲代码的小鱼2 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
Cobyte2 小时前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc