• vue2+echarts地图下钻+地图遮盖物散点


    一、下载工具

    npm i echarts echarts-gl axios -S

    -S是生产依赖默认是-S不写也可以 -D是开发依赖

    二、引入工具

    1. import * as echarts from "echarts";
    2. import "echarts-gl";
    3. import axios from "axios";

    三、HTML部分代码 

    1. <div class="iconText" v-show="address != '100000'">
    2. <svg
    3. style="cursor: pointer"
    4. @click="backMap"
    5. t="1681180771137"
    6. class="icon"
    7. viewBox="0 0 1024 1024"
    8. version="1.1"
    9. xmlns="http://www.w3.org/2000/svg"
    10. p-id="3427"
    11. width="40"
    12. height="40"
    13. >
    14. <path
    15. d="M426.666667 384V213.333333l-298.666667 298.666667 298.666667 298.666667v-174.933334c213.333333 0 362.666667 68.266667 469.333333 217.6-42.666667-213.333333-170.666667-426.666667-469.333333-469.333333z"
    16. p-id="3428"
    17. fill="#ffffff"
    18. >path>
    19. svg>
    20. <span style="font-size: 25px; font-weight: 200; color: #fff; text-shadow: 1px 2px 5px rgba(255, 255,255,.8);font-family: my-self-font;"
    21. >返回上级地图
    22. >
    23. div>
    24. <div class="map-chart" id="mapEchart">div>

    四、data函数中定义的数据 

    1. data() {
    2. return {
    3. // 地图下钻历史记录
    4. historyMapData: [{ name: "中国", adcode: "100000" }],
    5. address: "100000",
    6. addressName: "中国",
    7. district: "",
    8. showtab: false,
    9. scatterList: [],
    10. };
    11. },

    五、在methods方法中初始化一下dom

    1. chartMap() {
    2. // 初始化dom
    3. const myChart = echarts.init(document.getElementById("mapEchart"));
    4. // 初始化map
    5. this.initMap(myChart, "中国", "100000");
    6. // 添加点击事件
    7. myChart.on("click", (e) => {
    8. // 添加历史记录
    9. this.historyMapData.push(e.value);
    10. // 初始化地图
    11. this.initMap(myChart, this.addressName, e.value.adcode, e.value.schoolId);
    12. });

    六、让可视化地图跟随浏览器大小缩放

    1. window.addEventListener("resize", () => {
    2. myChart.resize();
    3. });

    七、下钻的时候清除一下echarts实例

    1. async initMap(chartDOM, geoName, adcode, id) {
    2. // 清除echarts实例
    3. chartDOM.clear();
    4. // 请求map的json
    5. const mapData = await this.getMapJSON(adcode, geoName, id);
    6. // 图表配置项
    7. const option = this.getOption(geoName, mapData);
    8. // 渲染配置
    9. if (this.district == "district") {
    10. option.series[1].data.push(
    11. { name: "杨家埠小学", value: [119.2, 36.85],schoolId:1 },
    12. { name: "杨家埠小学", value: [119.21, 37.01],schoolId:2 },
    13. { name: "杨家埠小学", value: [119.1, 36.9],schoolId:3 },
    14. { name: "杨家埠小学", value: [118.9, 37.15],schoolId:4 }
    15. );
    16. } else {
    17. option.series[1].data = [];
    18. }
    19. chartDOM.setOption(option);
    20. },

    八、获取阿里云地图数据

    1. async getMapJSON(address, geoName, id) {
    2. let res = null;
    3. if (address == undefined) {
    4. return null;
    5. }
    6. //判断地图下钻到district级的时候获取district数据
    7. if (this.district === "district") {
    8. res = await axios.get(
    9. `https://geo.datav.aliyun.com/areas_v2/bound/${address}.json`
    10. );
    11. } else if (this.district !== "district") {
    12. res = await axios.get(
    13. `https://geo.datav.aliyun.com/areas_v2/bound/${address}_full.json`
    14. );
    15. }
    16. // 重新注册地图
    17. echarts.registerMap(geoName, res.data);
    18. // 过滤json数据
    19. // 去掉台湾合海南岛边线
    20. if (
    21. res.data.features[20] &&
    22. res.data.features[20].properties.adcode == "460000"
    23. ) {
    24. res.data.features[20].geometry.coordinates.splice(1);
    25. }
    26. if (
    27. res.data.features[34] &&
    28. res.data.features[34].properties.adcode == "100000"
    29. ) {
    30. res.data.features[34].geometry.coordinates.splice(0);
    31. }
    32. const mapData = res.data.features.map((item) => {
    33. return {
    34. value: item.properties,
    35. name: item.properties.name,
    36. };
    37. });
    38. return mapData;
    39. },

    九、地图配置散点配置

    1. getOption(geoName, mapData) {
    2. const option = {
    3. geo3D: {
    4. zlevel: -100,
    5. show: false, //是否显示全部区域名称
    6. type: "map3D",
    7. roam: false,
    8. center: { x: 0, y: 0 },
    9. map: geoName, // 地图类型。echarts-gl 中使用的地图类型同 geo 组件相同
    10. regionHeight: 2,
    11. shading: "realistic",
    12. regions: [
    13. {
    14. name: mapData[0].name,
    15. itemStyle: {
    16. color: "#ff9900",
    17. },
    18. },
    19. ],
    20. shading: "lambert",
    21. //默认高亮区域
    22. emphasis: {
    23. label: { show: false },
    24. itemStyle: {
    25. color: "transparent",
    26. },
    27. },
    28. },
    29. series: [
    30. {
    31. zlevel: -10,
    32. regionHeight: 1,
    33. type: "map3D",
    34. viewControl: {
    35. panSensitivity: 0,
    36. rotateSensitivity: 0,
    37. // zoomSensitivity: 0,
    38. minAlpha: 90,
    39. },
    40. map: geoName, // 地图类型。echarts-gl 中使用的地图类型同 geo 组件相同
    41. data: mapData, //这里比较重要 获得过滤后的data,这样点击事件时就能获得这个data的值
    42. label: {
    43. show: false, // 是否显示标签。
    44. textStyle: {
    45. color: "#fff", // 地图初始化区域字体颜色
    46. fontSize: 14,
    47. fontWeight: 600,
    48. },
    49. formatter: (e) => {
    50. return ` ${e.name} `;
    51. },
    52. },
    53. shading: "realistic",
    54. realisticMaterial: {
    55. detailTexture: "./public/image.png",
    56. },
    57. itemStyle: {
    58. borderColor: "rgba(50, 123, 200, 0.5)", //区域边界线颜色
    59. borderWidth: 2, // 区域边界宽度
    60. color: "skyblue",
    61. opacity: 0.9,
    62. },
    63. emphasis: {
    64. label: {
    65. show: true, //鼠标划过或停留是否现在区域名称
    66. textStyle: {
    67. borderColor: "#f00",
    68. color: "#fff", //鼠标划过或停留的字体颜色
    69. },
    70. },
    71. itemStyle: {
    72. color: "#fff", //鼠标划过或停留的区域颜色
    73. },
    74. },
    75. },
    76. {
    77. zlevel: 1,
    78. type: "scatter3D", // 三维散点图
    79. coordinateSystem: "geo3D",
    80. data: [],
    81. roam: true,
    82. symbol: 'path://M51.911,16.242C51.152,7.888,45.239,1.827,37.839,1.827c-4.93,0-9.444,2.653-11.984,6.905 c-2.517-4.307-6.846-6.906-11.697-6.906c-7.399,0-13.313,6.061-14.071,14.415c-0.06,0.369-0.306,2.311,0.442,5.478 c1.078,4.568,3.568,8.723,7.199,12.013l18.115,16.439l18.426-16.438c3.631-3.291,6.121-7.445,7.199-12.014 C52.216,18.553,51.97,16.611,51.911,16.242z',
    83. // symbol: shcoolAddress,
    84. symbolSize: 20, // 散点大小
    85. label: {
    86. // 散点标签设置
    87. formatter: "{b}", // 显示数据点的名称
    88. position: "top",
    89. show: true,
    90. textStyle: {
    91. color: "#fff",
    92. padding: [5, 2],
    93. backgroundColor: {
    94. image: addressImg,
    95. },
    96. },
    97. },
    98. itemStyle: {
    99. // 散点样式设置
    100. color: "gold",
    101. },
    102. emphasis: {
    103. // 高亮状态样式设置
    104. label: {
    105. show: true,
    106. },
    107. itemStyle: {
    108. color: "skyblue",
    109. },
    110. },
    111. },
    112. ],
    113. };
    114. return option;
    115. },

    十、返回上级地图

    1. backMap() {
    2. const myChart = echarts.init(document.getElementById("mapEchart"));
    3. // 去除当前的地图信息
    4. this.historyMapData.pop();
    5. const len = this.historyMapData.length;
    6. // 获取上一级的地图信息
    7. const newdata = this.historyMapData[len - 1];
    8. // 重新渲染地图
    9. this.initMap(
    10. myChart,
    11. newdata?.name || "map",
    12. newdata?.adcode || "100000"
    13. );
    14. },

    十一、在mounted中调用chartMap

    1. mounted() {
    2. this.chartMap();
    3. },

  • 相关阅读:
    elasticdump官方教程
    Java 语言实现简易版扫码登录
    代码随想录算法训练营Day56 | 583. 两个字符串的删除操作 | 72. 编辑距离 | 编辑距离总结篇
    【Verilog基础】【计算机体系结构】多核cache一致性
    关于LoRa模块你需要知道的一切
    linux内核的文件组织形式
    vue2 使用 vue-video-player 播放m3u8 流地址视频
    CAS:1260586-88-6_生物素-C5-叠氮_Biotin-C5-Azide
    解决老版本Oracle VirtualBox 此应用无法在此设备上运行问题
    最大流与最小费用最大流简略版)
  • 原文地址:https://blog.csdn.net/weixin_66412671/article/details/139253274