vue-实现高德地图-省级行政区地块显示+悬浮显示+标签显示

news2024/9/25 3:18:06

在这里插入图片描述

<template>
  <div>
    <div id="container" />
    <div @click="showFn">显示</div>
    <div @click="removeFn">移除</div>
  </div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader'
import chinaData from './provincialData/中华人民共和国.json'
export default {
  data() {
    return {
      map: null,
      infoWindow: null,
      markers: [],
      provincPolygonList: []
    }
  },
  async mounted() {
    await this.initMap()
    // 初始完地图后,开始绘制
    await this.setUpPlotsFn(chinaData)
  },
  methods: {
    /** 1. 初始化地图 **/
    initMap() {
      return new Promise((resolve) => {
        window._AMapSecurityConfig = {
          securityJsCode: '你的安全密钥' // 自2021年12月02日升级后, key与安全密钥必须一起使用, 否则可能会出现一些API无法使用,如 DistrictSearch
        }
        AMapLoader.load({
          key: '697eb023cbaadfdf68211c7b18165ed7', // 首次调用 load 时必填
          version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
          plugins: [
            'AMap.DistrictSearch', // 配置行政区查询服务
            'AMap.GeoJSON' // 配置行政区查询服务
          ] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
        })
          .then((AMap) => {
            this.map = new AMap.Map('container', {
              center: [116.30946, 39.937629],
              zoom: 3
            })
            this.map.on('complete', () => {
              resolve()
            })
          })
          .catch((e) => {
            console.log(e)
          })
      })
    },

    // 设置地块
    setUpPlotsFn(featuresObj) {
      const { features } = featuresObj
      features.forEach((item) => {
        this.addGeoJsonFn(item)
      })
      this.map.add(this.provincPolygonList)
      this.map.add(this.markers)
      // 地图自适应
      this.map.setFitView()
    },
    // GeoJson数据进行解析
    addGeoJsonFn(feature) {
      const geoJSON = {
        type: 'FeatureCollection',
        features: [feature]
      }
      this.map.plugin('AMap.Geocoder', () => {
        const polygon = new window.AMap.GeoJSON({
          geoJSON: geoJSON,
          getPolygon: function(geojson, lnglats) {
            // 将解析出来的面 进行绘制
            return new window.AMap.Polygon({
              path: lnglats,
              fillOpacity: 0.4,
              fillColor: '#80d8ff',
              strokeColor: '#08B2BD',
              strokeWeight: 1,
              map: this.map
            })
          }
        })
        const { name, centroid } = feature.properties
        console.log('🚀 ~ file: MapConnentNewLast.vue:89 ~ this.map.plugin ~  feature.properties:', feature.properties)
        polygon.on('mouseover', (e) => {
          // 鼠标移入更改样式
          polygon.setOptions({
            fillOpacity: 0.7,
            fillColor: '#08B2BD'
          })
          const info = []
          info.push(
            `<div style="font-size: 12px; background-color: #fff;padding: 10px; border-radius: 10px;"><div style="font-weight: 700;">${name}</div>`
          )
          info.push(
            '<div style="display: flex; justify-content: space-between;align-items: center;padding: 4px 0;"><span style="color:#666;padding-right: 10px;">聚合资源总量</span> <span style="font-weight: 700;">100MW</span></div>'
          )
          info.push(
            '<div style="display: flex; justify-content: space-between;align-items: center;padding: 4px 0;"><span style="color:#666;">充电站</span> <span style="font-weight: 700;">100座</span></div>'
          )
          info.push(
            '<div style="display: flex;justify-content: space-between;align-items: center;padding: 4px 0;"><span style="color:#666;">换电站</span> <span style="font-weight: 700;">100座</span></div>'
          )
          info.push(
            '<div style="display: flex;justify-content: space-between;align-items: center;padding: 4px 0;"><span style="color:#666;">光伏</span> <span style="font-weight: 700;">100MW</span></div>'
          )
          info.push(
            '<div style="display: flex; justify-content: space-between;align-items: center;padding: 4px 0;"><span style="color:#666;">储能</span> <span style="font-weight: 700;">100MWH</span></div></div>'
          )
          this.infoWindow = new window.AMap.InfoWindow({
            isCustom: true, // 使用自定义窗体
            content: info.join(''),
            offset: new window.AMap.Pixel(0, -30)
          })
          // 获取点击的位置信息
          const lnglat = e.lnglat
          // 在点击的位置上显示信息窗体
          this.infoWindow.open(this.map, lnglat)
        })

        polygon.on('mouseout', () => {
          // 鼠标移出恢复样式
          this.infoWindow.close()
          polygon.setOptions({
            fillOpacity: 0.5,
            fillColor: '#80d8ff'
          })
        })

        this.provincPolygonList.push(polygon)

        const center = this.$turf.centroid(geoJSON).geometry.coordinates
        console.log('🚀 ~ file: MapConnentNewLast.vue:138 ~ this.map.plugin ~ center:', center)
        if (name) {
          this.addMarkerList(centroid || center, name)
        }
      })
    },

    addMarkerList(center, item) {
      const markersContent = []
      markersContent.push(`<div style="display: flex;align-items: center;font-size: 10px;border-radius: 4px;">`)
      markersContent.push(
        `<span style="color:#666;background-color: #fff;min-width: 40px;height: 20px;display: flex;align-items: center;justify-content:center">${item}</span>`
      )
      markersContent.push(
        `<span style="color:#fff;background-color: #3AD6C4;min-width: 40px;height: 20px;display: flex;align-items: center;justify-content:center">3213</span>`
      )
      markersContent.push(`</div>`)
      const marker = new window.AMap.Marker({
        position: center, // 标注点位置
        content: markersContent.join(''),
        map: this.map // 将标注点添加到地图上
      })
      this.markers.push(marker)
    },
    // 隐藏
    removeFn() {
      if (this.markers) {
        this.markers.forEach((item) => item.hide())
        this.provincPolygonList.forEach((item) => item.hide(item))
      }
    },
    // 隐藏
    showFn() {
      if (this.markers) {
        this.markers.forEach((item) => item.show())
        this.provincPolygonList.forEach((item) => item.show(item))
      }
    }
  }
}
</script>

<style scoped lang="scss">
#container {
  width: 100%;
  height: 53vh;
}

.infoWindow {
  display: flex;
  flex-direction: column;
  font-size: 10px;

  .title {
    color: #000;
  }

  .info_item {
    display: flex;
    justify-content: space-between;
    align-items: center;

    &:first-child {
      span {
        color: #444;

      }
    }
  }
}

.amap-info-content {
  border-radius: 10px;
}</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1315247.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SpringBoot - application.yml 多环境切换解决方案

问题描述 这个问题玩过 SpringCloud 的小伙伴估计会想到用 bootstrap.yml 来解决这个问题。但是如果说为了解决这个问题引入了一堆的 SpringCloud Jar&#xff0c;就感觉杀鸡用牛刀。 于是今天我们想只有 application.yml 自己就可以解决这个问题&#xff0c;如何搞定&#x…

真正可行的vue3迁移到nuxt3方法(本人亲测,完全避坑)

终于到了总结经验的时候了&#xff0c;这绝对是全网唯一、完全真正可行的干货。 在我看来&#xff0c;知识就是要拿来分享的&#xff0c;分享给他人也是在提高自己。我绝对不会搞什么订阅或者vip专栏来搞钱坑害各位&#xff0c; 因为我在csdn写文章最主要的目的是为了记录和总…

Chrome限制第三方Cookie:未来无法再追踪你看过哪些敏感的“色色”内容了

当我们在浏览网络的时候&#xff0c;常听到「Cookie」这个词&#xff0c;但许多人可能不太清楚它到底是什么。最近&#xff0c;Google 宣布了一项重要更新&#xff0c;Google Chrome 将减少对第三方cookie 的支持&#xff0c;以提高用户隐私保护。 下面我会解释一下这个改变对…

华为数通——路由冗余和备份

注&#xff1a;当一条路由的出接口down时&#xff0c;该路由会自动失效。 要求&#xff1a;数据优先走千兆链路。 R1 [ ]ip route-static 172.16.1.0 24 12.1.1.2 目的地址 掩码 下一条 [ ]ip route-static 172.16.1.0 24 21.1.1.2 preference 50 目的地址 …

ETLCloud的应用策略——实时数据处理是关键

一、ETLCloud是什么&#xff1f; ETLCloud又称数据集成&#xff08;DataOps&#xff09;&#xff0c;是RestCloud旗下的一款数据仓库管理工具&#xff0c;通过自动化数据转换和集成来实现企业内部和外部数据的无缝对接&#xff0c;从而帮助企业快速获取准确的数据信息&#xff…

vue3+leaflet天地图开发

<script setup> import { onMounted, onUnmounted, ref } from "vue"; // todo 项目使用请放开 leaflet 引入 // import L from leaflet;const emit defineEmits(["mapLoad"]);var markers ref([]); const mapRef ref(); const marker ref(); co…

Spring对JUnit4和junit5的支持

Junit4支持 第一步&#xff1a;准备工作&#xff1a; 引入JUnit4的依赖&#xff0c;Spring对JUnit支持的依赖还是&#xff1a;spring-test&#xff0c;如下&#xff1a; <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://ma…

Cadence SPB17.4 -Allegro - 做Logo封装及添加中文丝印

Cadence SPB17.4 -Allegro - 做Logo封装及添加中文丝印 Chapter1 Cadence SPB17.4 -Allegro - 做Logo封装Chapter2 Allegro添加中文字体的简单有效方法Chapter3 Allegro添加Logo方法方法一方法二 链接&#xff1a;https://pan.baidu.com/s/1eUgUOjOfNam3rqZyQOov_g 提取码&…

Java技术栈 —— Log4j 2、Logpack、SLF4j日志框架介绍

Log4j 2、Logpack、SLF4j日志框架介绍 Log4j 2、Logpack、SLF4j日志框架&#xff0c;及其区别1.1 Log4j 21.1.1 日志级别1.1.2 日志输出目标位置1.1.3 日志刷新机制1.1.4 结构化打印日志1.1.5 异步打印日志1.1.6 在Cloud云环境汇集日志信息 1.2 LogPack1.3 SLF4j1.4 区别 Log4j…

tcp连接全过程各种状态详解

文章目录 TCP的一些重要特性tcp连接全过程各种状态参考资料 TCP的一些重要特性 TCP是一种可靠、面向连接、全双工、流控制、拥塞控制、有序传输、无差错传输、无重复传输、无丢失传输等特点的协议。为了实现这些特点&#xff0c;TCP必须对上层应用程序发送的数据进行分段、重组…

玩转大数据18:大规模数据处理与分布式任务调度

引言 在数字化时代&#xff0c;数据成为了一种宝贵的资源&#xff0c;对于企业和组织来说&#xff0c;如何有效地处理和分析这些数据成为了关键的竞争力。大规模数据处理与分布式任务调度作为大数据处理的核心技术&#xff0c;为解决这一问题提供了有效的解决方案。 随着数据…

Python如何匹配库的版本

目录 1. 匹配库的版本 2. Python中pip&#xff0c;库&#xff0c;编译环境的问题回答总结 2.1 虚拟环境 2.2 pip&#xff0c;安装库&#xff0c;版本 1. 匹配库的版本 &#xff08;别的库的版本冲突同理&#xff09; 在搭建pyansys环境的时候&#xff0c;安装grpcio-tools…

阿赵UE学习笔记——1.安装UE

大家好&#xff0c;我是阿赵&#xff0c;这是一个新的开始&#xff0c;这次先简单介绍一下UE的安装方法。   安装Unreal Engine(简称UE)&#xff0c;需要先按照一个EPIC Games Launcher。 这个其实是EPIC的游戏中心&#xff0c;你可以理解成和Steam差不多&#xff0c;是一个…

盲盒小程序如何搭建?

随着移动互联网的发展&#xff0c;为了让消费者方便快捷地体验盲盒抽取乐趣&#xff0c;线上盲盒系统的开发成为了一个必要的过程。 今天本文将为大家介绍盲盒系统的搭建过程。 盲盒系统搭建过程 开发需求 在开发盲盒系统前&#xff0c;需要对盲盒市场深入分析&#xff0c;了…

IntelliJ IDEA 运行 若依分离版后端

一、本地运行 一、选择打开IntelliJ IDEA项目 二、选择若依项目 如&#xff1a;java123 三、等待右下角的准备工作&#xff08;有进度条的&#xff09;完成 四、修改MySQL 五、修改资源上传目录 六、修改redis 七、然后点击运行 八、成功图 九、测试访问 二、部署服务器运行 …

Linux-----12、时间日期

# 时间日期 # 时区设置 在Linux (opens new window)系统中&#xff0c;默认使用的是UTC时间。 即使在安装系统的时候&#xff0c;选择的时区是亚洲上海&#xff0c;Linux默认的BIOS时间&#xff08;也称&#xff1a;硬件时间&#xff09;也是UTC时间 (opens new window)。 在…

uniapp 单选按钮 选中默认设备

需求1&#xff1a;选中默认设备&#xff0c;113 和114 和139都可以选中一个默认设备 选中多个默认设备方法&#xff1a; async toSwitch(typeItem, title) {const res await this.setDefaultDev(typeItem.ibdr_devsn, typeItem.ibdr_pid)if (!res) {this.common.toast(切换默…

011 数据结构_哈希

前言 本文将会向你介绍哈希概念&#xff0c;哈希方法&#xff0c;如何解决哈希冲突&#xff0c;以及闭散列与开散列的模拟实现 1. 哈希概念 顺序结构以及平衡树中&#xff0c;元素关键码与其存储位置之间没有对应的关系&#xff0c;因此在查找一个元素时&#xff0c;必须要经…

Spring容器中scope为prototype类型Bean的回收机制

文章目录 一、背景二、AutowireCapableBeanFactory 方法 autowireBean 分析三、Spring 容器中 scope 为 prototype 类型 Bean 的回收机制四、总结 一、背景 最近做 DDD 实践时&#xff0c;遇到业务对象需要交给 Spring 管理才能做一些职责内事情。假设账号注册邮箱应用层代码流…

【ARM Trace32(劳特巴赫) 使用介绍 1.2 - ARM 系统调试中常见的挑战】

请阅读【Trace32 ARM 专栏导读】 文章目录 ARM 系统调试中常见的挑战ARM 系统调试接口简例DAP-Debug Access portDAP 状态检查多核调试虚拟/物理地址Cache 数据一致性问题系统异常系统复位系统死机PC 采样Memory 采样RAM/Core Dump 分析小概率问题ARM 系统调试中常见的挑战 调试…