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>
三、效果图如下
相关推荐
小镇学者6 小时前
【JS】element-ui table展示勾选状态
javascript·vue.js·ui
冰羽IOX7 小时前
Nginx部署Umi React前端项目标准配置
前端·nginx·react.js·umi·antd
恰小面包7 小时前
react的antd表格自定义图标
前端·javascript·react.js
资深前端之路7 小时前
react面试题三
前端·vue.js·react.js
优联前端9 小时前
Web 音视频(四)在浏览器中处理音频
开发语言·javascript·音视频·优联前端·webav·webcodecs
【 】4239 小时前
安卓7以上抓包证书安装
android·javascript·爬虫·网络爬虫
明似水9 小时前
高效管理Dart和Flutter多包项目:Melos工具全解析
android·前端·flutter
helianying5510 小时前
拥抱开源,助力创新:IBM永久免费云服务器助力开源项目腾飞
运维·服务器·前端·开源
wl851110 小时前
Vue 入门到实战 八
前端·javascript·vue.js