echarts加载地图

echarts加载地图

一、参考文档

1、官网地址:https://echarts.apache.org/zh/option.html#series-map

2、地图json下载地址:https://datav.aliyun.com/portal/school/atlas/area_selector#\&lat=31.769817845138945\&lng=104.29901249999999\&zoom=4

二、加载地图
2.1加载中国地图,并添加飞线
  • 准备中国地图数据,

  • 准备飞线数据

    json 复制代码
    //飞线格式数据如下 
    [{
              coords: [
                [startLongitude, startLatitude],
                [endLongitude, endLatitude],
              ],
              value:'',
            }]
2.2代码如下
vue 复制代码
<template>
  <div class="container">
    <div id="map" ref="chartRef"></div>
    </div>
</template>
<script lang="ts" setup>
    import { onMounted, ref, reactive, onUnmounted } from "vue";
import * as echarts from "echarts";
import chinaMapData from "chinaMap.json"; //本地中国地图数据
import dailyFlyData from "dailyFlyData.json";//本地飞线数据
    
    const chartRef = ref<HTMLDivElement | null>(null);

let chart: echarts.ECharts | null = null;
    
    // 初始化地图
function initChart() {
  if (chartRef.value) {
    chart = echarts.init(chartRef.value);
    // 注册地图数据
    echarts.registerMap('china', chinaMapData);
    renderMap();
  }
}
    //加载地图
    function renderMap(mapName: string) {
  try {
    if (!chart) return;  
    chart.clear();
    chart.setOption(getChineConfig());
  } catch (error) {
    console.error("Error rendering map:", error);
  }
}
    
    //加载中国地图
function getChineConfig() {
  const chinaOption = {
    tooltip: {
      enterable: true,
      transitionDuration: 1,
      textStyle: {
        color: "#fff",
        fontSize: 13,
      },
      backgroundColor: "rgba(15,33,70,0.7)",
      borderColor: "rgba(54,82,132,0.7)",
      borderWidth: 1,
      padding: [10, 14],
      extraCssText: "z-index:4",
      formatter: function (parma: any) {
        if (parma.seriesType === "effectScatter") {
          return parma.name;
        }
      },
    },
      //添加地图阴影,呈现立体效果
    geo: {
      zoom: 1, // 缩放比例
      show: true,
      aspectScale: 0.85,
      silent: true, // 鼠标事件是否开启
      animation: true,
      map: "china",
      label: {
        show: false,
      },
      roam: false, //是否可缩放
      regions: [
        {
          name: "南海诸岛",
          itemStyle: {
            opacity: 0, // 为 0 时不绘制该图形
          },
          label: {
            show: false, // 隐藏文字
          },
        },
      ],
      itemStyle: {
        borderColor: "#27e0fd",
        shadowColor: "#27e0fd",
        shadowOffsetX: 1,
        shadowOffsetY: 9,
      },
      emphasis: {
        itemStyle: {
          areaColor: "#4499d0",
          color: "#fff",
        },
        label: {
          show: true,
          color: "#fff", // 确保强调状态下省份名称为白色
        },
      },
    },
    series: [
      {
        type: "map",
        map: "china",
        zoom: 1, // 缩放比例
        geoIndex: 1,
        aspectScale: 0.85, // 长宽比
        showLegendSymbol: true, // 存在legend时显示
        label: {
          show: false,
        },
        emphasis: {
          label: {
            show: true,
            color: "#fff", // 确保强调状态下省份名称为白色
          },
          itemStyle: {
            areaColor: "#1affa3",
            color: "#fff",
          },
        },
        roam: false,
        itemStyle: {
          color: "none",
          areaColor: "rgba(43,203,240,1)",
          borderColor: "#27e0fd",
          borderWidth: 1,
        },
        select: {
          label: {
            color: "#fff",
          },
          itemStyle: {
            areaColor: "#34d1fc",
          },
        },
        animation: true,
        data: [],
      },
        //加载飞线
      {
        type: "lines",
        zlevel: 1,
        effect: {
          show: true,
          period: 6,
          trailLength: 0.02,
          symbol: "arrow",
          symbolSize: 6,
        },
        label: {
          show: true,
          position: "end",
          fontSize: 14,
          color: "#fff",
        },
        emphasis: {
          show: false,
        },
        lineStyle: {
          width: 1.5,
          color: "rgb(249, 252, 22)",
          opacity: 1,
          curveness: -0.3,
        },
          //将飞线数据处理成需要的数据格式
        data: dailyFlyData.map((item) => ({
          coords: [
            [item.startLongitude, item.startLatitude],
            [item.endLongitude, item.endLatitude],
          ],
          value: item.name,
        })),
      },
    ],
  };

  return chinaOption;
}
    onMounted(() => {
  initChart();
  if (chart) {
    chart.on("click", handleMapClick); // 监听点击事件
    //监听空白处点击事件
    chart.getZr().on('click', function(event) {
  // 没有 target 意味着鼠标/指针不在任何一个图形元素上,它是从"空白处"触发的。
  if (!event.target) {
    // 点击在了空白处,做些什么。
  }
});
  }
});
onUnmounted(() => {
  chart?.clear();
});
</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
  position: relative;
}
/* 设置地图容器的样式 */
#map {
  width: 100%;
  height: 100%;
}
</style>
三、效果图如下
相关推荐
bai2731 小时前
path.join('C:\a', '\b') 给我 C:\a\b,path.resolve 给 C:\b:Windows 上 51 行路径实测
javascript·后端
kyriewen1 小时前
我写的 12 个 AI Skill,三个月后 8 个在吃空饷
javascript·人工智能·程序员
北城笑笑2 小时前
Vue 112 ,下篇:前端 dist 还原成 Vue3 + Vite 项目( krpano 全景接入与页面适配实战,Vue2 → Vue3 )
前端·javascript·vue.js·3d·前端框架·vue·webgl
我叫黑大帅2 小时前
搜“张三”把备注也带出来了?不用加高级筛选,如何让一个搜索框实现「选项直达」!
前端·javascript·面试
KevinWang_3 小时前
iOS 开发计划(三) - 打印 JavaScript 调试日志
javascript
如夏倾城4 小时前
WAF (Web Application Firewall,Web应用防火墙)
前端·网络·安全·web安全·web app
图扑可视化5 小时前
基于图扑软件智慧制硅厂 Web SCADA 开发实践
前端·三维可视化·scada·hmi·组态软件
前端fighter6 小时前
2026年前端面试题总结(pdf版)
前端·pdf
谢小飞6 小时前
运维加了个参数,我们的网页挂掉了
前端·浏览器
CoderYanger7 小时前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis