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加载中国地图,并添加飞线
  • 准备中国地图数据,

  • 准备飞线数据

    //飞线格式数据如下 
    [{
              coords: [
                [startLongitude, startLatitude],
                [endLongitude, endLatitude],
              ],
              value:'',
            }]
    
2.2代码如下
<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>
三、效果图如下

在这里插入图片描述

ECharts是一个强大的JavaScript数据可视化库,它支持加载各种地图数据并绘制图表。在ECharts中添加地图通常是通过地理坐标系统(Geographic Coordinate System,简称GCJ02或WGS84)来定位数据点。 首先,你需要在ECharts官网下载包含中国地图的数据文件,如`world.json`或者`china.js`等。然后,在配置选项中设置地图相关属性: ```javascript // 初始化ECharts实例 var myChart = echarts.init(document.getElementById('main')); // 加载地图数据 myChart.options = { geo: { // 地图配置 type: 'map', // 设置地图类型 mapType: 'china', // 指定使用的地图文件,例如中国的省份地图 roam: true, // 是否开启平滑缩放和移动效果 label: { // 标记标签 show: false // 隐藏默认的省级名称 }, tooltip: { // 制作鼠标悬停提示信息 trigger: 'item' } }, series: [ // 系列配置,这里我们创建一个新的系列用于绘制曲线 { name: '曲线图', type: 'lines', // 设置系列类型为折线图 data: [], // 初始为空,待填充实际的数据点 geoIndex: 0, // 设置对应地图的索引,因为echarts在同一层级会同时处理地图和其他系列 coordinateSystem: 'geo' // 指定使用地理坐标系 } ] }; // 然后你可以动态添加或更新数据,比如从API获取到的数据 // 示例数据 var dataPoints = [ ['北京', 100], ['上海', 200], ['广州', 150] ]; dataPoints.forEach(function(item) { var province = item[0]; // 省份名 var value = item[1]; // 数据值 // 更新series.data数组,将省份名和值对映射到地理坐标上 myChart.setOption({ series: { data: myChart.options.series[0].data.concat([{name: province, value: value}]) } }); }); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值