vue+openlayers 点聚合,点击弹窗

1.获取的聚合数据

getMapData(label){
  agriculturalParkMap().then((res) => {
    if (res.code === 200){
      if(label=='种植大户'){
        this.plantMarkerList = res.data.parkMap2
      }else {
        this.plantMarkerList = res.data.parkMap1
      }
      this.setCluster(this.plantMarkerList,label)
    }
  })
},

2.实现聚合

setCluster(points,label){
  var count = points.length;
  var features = new Array(count);
  for (var i = 0; i < count; i++) {
    var coordinate = points[i].lnglat;
    
    if(label=='种植大户'){
      var attr = {
        lnglat: points[i].lnglat,
        town_name: points[i].town_name,
        region: points[i].region,
        contacts: points[i].contacts,
        phone: points[i].phone,
        plant_area: points[i].plant_area,
        average_yield: points[i].average_yield,
        main_food_crops: points[i].main_food_crops
      }
    }else {
      var attr = {
        lnglat: points[i].lnglat,
        park_name: points[i].park_name,
        region: points[i].region,
        park_position: points[i].park_position,
        concats: points[i].concats,
        phone: points[i].phone,
        prime_mover_industry: points[i].prime_mover_industry,
        existing_facilities: points[i].existing_facilities,
        trafficWay: points[i].trafficWay,
      }
    }

    features[i] = new ol.Feature({
      geometry: new ol.geom.Point(coordinate),
      attribute: attr,
    });
  }

  // 创建几个聚合效果所在的点
  var source = new ol.source.Vector({
    features: features
  })

  // Cluster聚合类
  var clusterSource = new ol.source.Cluster({
    distance: 90,  // 聚合点与点之间的距离
    source: source,
  })

  this.vectorLayer = new ol.layer.Vector({
    source: clusterSource,
    // 聚合样式
    style: function (feature) {
      var img,img1,scale,anchor
      if(label=='种植大户'){
        img=require("../../img/livestockManagement/111.png")
        img1=require('@/img/planting/main5/zz.png')
        scale=0.74
        anchor=[0.5, 25]
      }else {
        img=require("../../img/machineryManagement/main2/yuan.png")
        img1=require('@/img/planting/main5/ny.png')
        scale=0.45
        anchor=[0.5, 40]
      }
      // 点的个数
      var size = feature.get('features').length
      if(size > 1){
        return new ol.style.Style({
          image: new ol.style.Icon({
            anchor: anchor,
            scale:scale,  //大小
            offset: [0, 0], //偏移量设置
            anchorXUnits: 'fraction',
            anchorYUnits: 'pixels',
            src: img
          }),

          text: new ol.style.Text({ // 文字样式
            font: '12px sans-serif',
            text: size.toString(),
            fill: new ol.style.Fill({
              color: '#fff'
            })
          })
        })
      }else {
        return new ol.style.Style({
          image: new ol.style.Icon({
            // anchor: [0.5, 46],
            scale: 1.7,  //大小
            offset: [0, 0], //偏移量设置
            anchorXUnits: 'fraction',
            anchorYUnits: 'pixels',
            src: img1
          }),

        })
      }
    }
  })
  this.map.addLayer(this.vectorLayer);
},

3.弹窗

<div ref="plantDialog" class="plantDialog">
      <div class="my-container">
        <div class="title">
          <span class="text">种粮大户</span>
          <i class="el-icon-close icon1" style="top:4%;right:4%" @click="closeDialog"></i>
        </div>
        <div class="center">
          <div class="content">
            <div class="label">所属镇街:</div>
            <div class="value">{{ singleData.town_name}}</div>
          </div>
          <div class="content">
            <div class="label">单位或农户:</div>
            <div class="value">{{ singleData.region}}</div>
          </div>
          <div class="content">
            <div class="label">联系人:</div>
            <div class="value">{{ singleData.contacts}}</div>
          </div>
        <div>
</div>

初始化地图的时候,绑定弹窗

 this.overlay = new ol.Overlay({
          element: this.$refs.plantDialog,
          autoPan: true,
          offset: [-240, -250],
          autoPanAnimation: {
            duration: 250
          }
        });
    

  this.map = new ol.Map({
          target: 'plantMap3',
          controls: ol.control.defaults({
            attributionOptions: {
              collapsed: true
            },
            zoom: false,
            rotate: false,
            attribution: false,
            scaleLine: true
          }),
          view: new ol.View({
            center: [119.873281, 35.859051],
            zoom: 11,
            projection: 'EPSG:4326'
          }),
          overlays: [this.overlay],
          this.map.on('singleclick', this.addClickEvent);
        });

4.点击弹窗

addClickEvent(e) {
        var isClickMarker = false
        this.singleData={}
        this.singleDataFarm={}
        var pixel = this.map.getEventPixel(e.originalEvent);
        this.map.forEachFeatureAtPixel(pixel, (feature) => {
          var attr = feature.getProperties();
          if(attr.features){
            isClickMarker = true
            if(attr.features.length == 1){
              if(this.legend=='种植大户'){
                this.singleData={}
                this.singleData = attr.features[0].A.attribute
                this.overlay.setPosition(attr.geometry.flatCoordinates)
              }else {
                this.singleDataFarm={}
                this.singleDataFarm = attr.features[0].A.attribute
                this.$nextTick(() => {
                  this.initChart()
                });
                this.overlay1.setPosition(attr.geometry.flatCoordinates)
              }

            }
          }
        })
}

5.关闭弹窗

closeDialog() {
  this.overlay.setPosition(undefined)
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Vue 3 中结合 Ant Design Vue 和 TypeScript 实现点击文字出现弹窗的功能,你可以按照以下步骤进行操作: 1. 确保你已经按照官方文档的指引正确安装和配置了 Vue 3、Ant Design Vue 和 TypeScript。 2. 创建一个 Vue 组件,例如 `MyComponent.vue`,并在其中引入必要的组件和样式: ```vue <template> <div> <p @click="showModal">点击这里弹出弹窗</p> <a-modal v-model:visible="visible" title="弹窗标题"> <p>弹窗内容</p> </a-modal> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import { Modal } from 'ant-design-vue'; export default defineComponent({ components: { 'a-modal': Modal, }, data() { return { visible: false, }; }, methods: { showModal() { this.visible = true; }, }, }); </script> <style scoped> /* 样式可以根据实际需求进行修改和定制 */ p { cursor: pointer; } </style> ``` 在这个示例中,我们创建了一个段落元素,并通过 `@click` 监听点击事件,当点击文字时调用 `showModal` 方法来显示弹窗。Modal 组件的使用方式与之前提到的示例相似。 3. 在需要使用该组件的地方引入并使用它: ```vue <template> <div> <my-component></my-component> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import MyComponent from './MyComponent.vue'; export default defineComponent({ components: { 'my-component': MyComponent, }, }); </script> ``` 通过在需要使用的地方引入 `MyComponent`,你就可以在页面上展示并使用点击文字弹窗的功能了。 请注意,这只是一个简单的示例,你可以根据自己的需求进行进一步的定制和扩展。希望这对你有帮助!如果你还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值