openlayers 实现 聚合点删除 和聚合点弹框的效果

openlayers 实现 聚合点删除 和聚合点弹框的效果

我的地图用的是父传子的方法写的 在这里插入图片描述

<template>
  <div>
  //弹框
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <!-- <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span> -->
    </el-dialog>
  </div>
</template>

<script>
import { Circle as CircleStyle } from "ol/style";
import { Map, View, Feature, Overlay } from "ol";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
import { Point } from "ol/geom";
import Style from "ol/style/Style";
import Fill from "ol/style/Fill";
import Stroke from "ol/style/Stroke";
import Text from "ol/style/Text";
import { Vector as SourceVec, Cluster } from "ol/source";

export default {
  props: ["map"],
  data() {
    return {
      size: "1",
      layer: null,
      sourceArr: null,
      dialogVisible: false,
    };
  },
  mounted() {
    this.addMarker();
    this.singleclick();
  },
  methods: {
    addMarker() {
      //先清除
      if (this.layer) {
        let flag = this.layer.getVisible();
        this.layer.setVisible(!flag);
      } else {
        const map = window.map;
        //创建画板
        this.sourceArr = new SourceVec({});
        //定义随机数据,这里随机了200个
        for (let i = 1; i <= 200; i++) {
          // console.log("进");
          //点的坐标信息
          let coordinates = [120.0 + Math.random(), 30.0 + Math.random()];
          let feature = new Feature(new Point(coordinates));
          let markerStyle = new Style({
            image: new CircleStyle({
              radius: 4,
              stroke: new Stroke({
                color: "#ffffff",
                width: 1,
              }),
              fill: new Fill({
                color: "blue",
              }),
            }),
            text: new Text({
              scale: 1.4,
              // text: size.toString(),
              // text: String(i),
              fill: new Fill({
                color: "#ffffff",
              }),
              offsetX: 30,
              offsetY: 0,
            }),
          });
          feature.setStyle(markerStyle);
          this.sourceArr.addFeature(feature);
        }
        //LayerVec /VectorLayer  这两种都可以
        this.layer = new VectorLayer({
          source: this.sourceArr,
          name: "xxx",
        });
        //地图添加画板
        map.addLayer(this.layer);
      }
    },
    //设置图层显示或者隐藏  flag  true 显示  false 隐藏
    setVisibleZSQWFXLayer(flag) {
      this.layer.setVisible(flag);
    },

    //清除逐时气温分析绘制的点
    clearZSQWFXLayer() {
      if (this.layer) {
        this.layer.getSource().clear();
      }
    },
    //点击事件
    singleclick() {
      const map = window.map;
      // 点击
      map.on("singleclick", (e) => {
        // 判断是否点击在点上
        let feature = map.forEachFeatureAtPixel(e.pixel, (feature) => feature);
        if (feature) {
          this.dialogVisible = true;
          // 设置弹窗位置
          let coordinates = feature.getGeometry().getCoordinates();
         // this.popup.setPosition(coordinates);
        } else {
          this.dialogVisible = false;
        }
      });
    },
    handleClose() {
      this.dialogVisible=false
    },
  },
};
</script>

<style  scoped>
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用OpenLayers实现聚合的完整代码示例。 HTML文件: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>OpenLayers Point Clustering Example</title> <link rel="stylesheet" href="https://openlayers.org/en/v6.5.0/css/ol.css" type="text/css"> <style> .map { height: 500px; width: 100%; } </style> </head> <body> <div id="map" class="map"></div> <script src="https://openlayers.org/en/v6.5.0/build/ol.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4-src.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/turf.js/5.1.6/turf.min.js"></script> <script src="cluster.js"></script> </body> </html> ``` JavaScript文件(cluster.js): ```javascript // 聚合半径 var radius = 40; // 创建地图 var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([0, 0]), zoom: 2 }) }); // 创建一个聚合层 var clusterLayer = new ol.layer.Vector({ source: new ol.source.Vector(), style: function(feature) { var size = feature.get('features').length; var style = new ol.style.Style({ image: new ol.style.Circle({ radius: 10, stroke: new ol.style.Stroke({ color: '#fff' }), fill: new ol.style.Fill({ color: '#3399CC' }) }), text: new ol.style.Text({ text: size.toString(), fill: new ol.style.Fill({ color: '#fff' }) }) }); return style; } }); map.addLayer(clusterLayer); // 加载数据并进行聚合 var url = 'data.geojson'; fetch(url).then(function(response) { return response.json(); }).then(function(json) { var features = new ol.format.GeoJSON().readFeatures(json); var clusters = getClusters(features, radius); clusterLayer.getSource().addFeatures(clusters); }); ``` 在上述代码中,我们首先创建了一个地图,并添加了一个OSM图层。然后创建了一个聚合层,并将其添加到地图中。接着,我们从一个GeoJSON文件中加载数据,并调用getClusters()函数对数据进行聚合。最后,我们将聚合后的数据添加到聚合层的源中。 请注意,在这个示例代码中,我们使用了turf.js库来计算两个点之间的距离,以便进行聚合。如果您不想使用turf.js,可以使用OpenLayers的ol.Sphere类来计算距离。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值