echarts+Vue2 绘制2D地图,添加散点,点击散点

举例:显示河北省地图

根据DataV.GeoAtlas地理小工具系列,获取到河北省的地图 .json数据。

例如获取到的河北省 json 地址是https://geo.datav.aliyun.com/areas_v3/bound/130000_full.json

将其在浏览器打开,在项目中新建一个hebei.json文件,复制全部数据到hebei.json。

然后新建.vue文件,在其中引入即可,具体代码如下:(复制即可直接食用,注意需要全局注册echarts)

<template>
  <div class="container">
    <div ref="myChart" style="width: 100%; height: 100%"></div>
  </div>
</template>

<script>
import hebei from "../../../public/extend/map/json/province/hebei.json";
export default {
  components: {},
  props: {},
  data() {
    return {
      chart: null,
      hebei,
      regions: [],
      tableData: [],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  methods: {
    initChart() {
      let that = this;
      that.$echarts.registerMap("hebei", this.hebei);
      // 散点数据
      var data = [
        {
          name: "河北科技大学",
          areaname: "河北科技大学",
          value: ["114.50963", "37.97686"],
          date: "2025-10-01",
          title: "石家庄",
        },
      ];
      that.tableData = data;
      var option = {
        tooltip: {
          trigger: "item",
        },
        visualMap: {
          show: true,
          min: 0,
          max: 200,
          left: "left",
          top: "bottom",
          text: ["高", "低"], // 文本,默认为数值文本
          calculable: true,
          seriesIndex: [1],
          inRange: {
            color: ["#00467F", "#A5CC82"], // 蓝绿
          },
        },
        geo: {
          map: "hebei",
          roam: false,
          zoom: 1.0,
          aspectScale: 1,
          scaleLimit: {
            min: 1,
            max: 2,
          },
          top: 35,
          right: 150,
          left: "auto",
          selectedMode: "single",
          select: {
            disabled: true,
            itemStyle: {
              borderWidth: 2.5,
              areaColor: "rgb(8, 207, 221)",
              shadowColor: "rgba(255, 255, 255, 0.3)",
              shadowBlur: 10,
              shadowOffsetX: 4,
              shadowOffsetY: 4,
            },
            label: {
              color: "rgba(255, 255, 255, 1)",
            },
          },
          regions: that.tableData,
          label: {
            show: true, // 是否显示地图上的所有地名
            color: "rgba(255, 255, 255, 0.6)",
            fontWeight: "bold",
            fontSize: 16,
          },
          itemStyle: {
            normal: {
              borderColor: "rgba(147, 235, 248, 1)",
              borderWidth: 1,
              areaColor: {
                type: "radial",
                x: 0.5,
                y: 0.5,
                r: 0.8,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgba(147, 235, 248, 0)", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "rgba(147, 235, 248, .2)", // 100% 处的颜色
                  },
                ],
                globalCoord: false, // 缺省为 false
              },
              shadowColor: "rgba(128, 217, 248, 1)",
              // shadowColor: 'rgba(255, 255, 255, 1)',
              shadowOffsetX: -2,
              shadowOffsetY: 2,
              shadowBlur: 10,
            },
            emphasis: {
              areaColor: "#389BB7",
              borderWidth: 0,
            },
          },
          emphasis: {
            disabled: false,
            focus: "none",
            label: {
              color: "rgba(255, 255, 255, 1)",
              fontWeight: "bold",
              fontSize: 16,
            },
            itemStyle: {
              borderWidth: 2.5,
              borderColor: "#9AD0F7",
              areaColor: "#962029",
              shadowColor: "rgba(255, 255, 255, 0.3)",
              shadowBlur: 10,
              shadowOffsetX: 4,
              shadowOffsetY: 4,
            },
          },
          tooltip: {
            show: true,
            textStyle: {
              color: "#FFF",
            },
            backgroundColor: "#011e49",
            borderColor: "transparent",
            borderWidth: 0,
            textShadowColor: "transparent",
            formatter: function (params) {
              let cityitem = that.tableData.find((item) => {
                return item.areaname === params.name;
              });
              // console.log('data', cityitem);
              if (typeof cityitem === "undefined") return "";
              let returnHtml = `
                <div style="font-size: 20px;font-weight: bold;margin-bottom: 6px;">${cityitem.areaname}</div>
                <div style="font-size: 16px;margin-bottom: 22px;">${cityitem.title}</div>
                <div style="font-size: 18px;margin-bottom: 14px;">${cityitem.value}(个)</div>
              `;
              return returnHtml;
            },
          },
        },
        series: [
          {
            name: "散点",
            type: "scatter",
            coordinateSystem: "geo",
            data: data,
            symbol: "pin",
            symbolSize: 30,
            label: {
              normal: {
                show: false,
              },
              emphasis: {
                show: false,
              },
            },
            itemStyle: {
              normal: {
                color: "#33C481",
              },
              emphasis: {
                color: "#f00",
              },
            },
          },
        ],
      };
      if (that.chart === null) {
        that.chart = that.$echarts.init(this.$refs.myChart);
      }
      that.chart.setOption(option);
      that.chart.on("click", (params) => {
        console.log("点击到了");

        if (
          params.componentType === "series" &&
          params.componentSubType === "scatter"
        ) {
          const index = params.dataIndex;
          // 取消所有散点高亮
          that.chart.dispatchAction({
            type: "downplay",
            seriesIndex: 0, //第几条series
          });
          // 显示指定data 的tooltip
          // myChart.dispatchAction({
          //     type: 'showTip',
          //     seriesIndex: 0, //第几条series
          //     dataIndex: index, //第几个tooltip
          // });
          // 高亮指定的散点
          that.chart.dispatchAction({
            type: "highlight",
            seriesIndex: 0, //第几条series
            dataIndex: index, //第几个tooltip
          });
        }
      });

      window.addEventListener("resize", function () {
        that.chart.resize();
      });
    },
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
    }
  },
};
</script>
<style scoped>
.container {
  width: 1006px;
  height: 696px;
}
</style>
地图贴图:

图片例子:

代码示例:(复制即可食用)

根据DataV.GeoAtlas地理小工具系列,获取到全国地图 china.json 数据。

代码中的 tuceng.jpeg 是上面的第一张图

<template>
  <div class="container">
    <div ref="myChart" style="width: 100%; height: 100%"></div>
  </div>
</template>
 
<script>
import hebei from "../../public/extend/map/json/province/china.json";
export default {
  components: {},
  props: {},
  data() {
    return {
      chart: null,
      hebei,
      regions: [],
      tableData: [],
      mapBg: require("../assets/tuceng.jpeg"),
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  methods: {
    initChart() {
      let that = this;
      that.$echarts.registerMap("hebei", this.hebei);
      let mapBg = document.createElement("img");
      mapBg.src = this.mapBg;
      // 散点数据
      var data = [
        {
          name: "河北科技大学",
          areaname: "河北科技大学",
          value: ["114.50963", "37.97686"],
          date: "2025-10-01",
          title: "石家庄",
        },
      ];
      that.tableData = data;
      var option = {
        tooltip: {
          trigger: "item",
        },
        visualMap: {
          show: true,
          min: 0,
          max: 200,
          left: "left",
          top: "bottom",
          text: ["高", "低"], // 文本,默认为数值文本
          calculable: true,
          seriesIndex: [1],
          inRange: {
            color: ["#00467F", "#A5CC82"], // 蓝绿
          },
        },
        geo: {
          map: "hebei",
          roam: true,
          zoom: 1.0,
          aspectScale: 1,
          scaleLimit: {
            min: 1,
            max: 2,
          },
          selectedMode: "single",
          select: {
            disabled: true,
            itemStyle: {
              borderWidth: 2.5,
              areaColor: "rgb(8, 207, 221)",
              shadowColor: "rgba(255, 255, 255, 0.3)",
              shadowBlur: 10,
              shadowOffsetX: 4,
              shadowOffsetY: 4,
            },
            label: {
              color: "rgba(255, 255, 255, 1)",
            },
          },
          regions: that.tableData,
          label: {
            show: true, // 是否显示地图上的所有地名
            color: "rgba(255, 255, 255, 0.6)",
            fontWeight: "bold",
            fontSize: 16,
          },
          itemStyle: {
            normal: {
              areaColor: {
                image: mapBg, // 支持为 HTMLImageElement, HTMLCanvasElement,不支持路径字符串
                repeat: "repeat", // 是否平铺, 可以是 'repeat-x', 'repeat-y', 'no-repeat'
              }
            },
            emphasis: {
              areaColor: {
                // 支持为 HTMLImageElement, HTMLCanvasElement,不支持路径字符串
                image: mapBg,
                // 是否平铺, 可以是 'repeat-x', 'repeat-y', 'no-repeat'
                repeat: "repeat"
              },
            },
          },
          emphasis: {
            disabled: false,
            focus: "none",
            label: {
              color: "rgba(255, 255, 255, 1)",
              fontWeight: "bold",
              fontSize: 16,
            },
            itemStyle: {
              borderWidth: 2.5,
              borderColor: "#9AD0F7",
              areaColor: "#962029",
              shadowColor: "rgba(255, 255, 255, 0.3)",
              shadowBlur: 10,
              shadowOffsetX: 4,
              shadowOffsetY: 4,
            },
          },
          tooltip: {
            show: true,
            textStyle: {
              color: "#FFF",
            },
            backgroundColor: "#011e49",
            borderColor: "transparent",
            borderWidth: 0,
            textShadowColor: "transparent",
            formatter: function (params) {
              let cityitem = that.tableData.find((item) => {
                return item.areaname === params.name;
              });
              // console.log('data', cityitem);
              if (typeof cityitem === "undefined") return "";
              let returnHtml = `
                <div style="font-size: 20px;font-weight: bold;margin-bottom: 6px;">${cityitem.areaname}</div>
                <div style="font-size: 16px;margin-bottom: 22px;">${cityitem.title}</div>
                <div style="font-size: 18px;margin-bottom: 14px;">${cityitem.value}(个)</div>
              `;
              return returnHtml;
            },
          },
        },
        series: [
          {
            name: "散点",
            type: "scatter",
            coordinateSystem: "geo",
            data: data,
            symbol: "pin",
            symbolSize: 30,
            label: {
              normal: {
                show: false,
              },
              emphasis: {
                show: false,
              },
            },
            itemStyle: {
              normal: {
                color: "#33C481",
              },
              emphasis: {
                color: "#f00",
              },
            },
          },
        ],
      };
      if (that.chart === null) {
        that.chart = that.$echarts.init(this.$refs.myChart);
      }
      that.chart.setOption(option);
      that.chart.on("click", (params) => {
        console.log("点击到了");

        if (
          params.componentType === "series" &&
          params.componentSubType === "scatter"
        ) {
          const index = params.dataIndex;
          // 取消所有散点高亮
          that.chart.dispatchAction({
            type: "downplay",
            seriesIndex: 0, //第几条series
          });
          // 显示指定data 的tooltip
          // myChart.dispatchAction({
          //     type: 'showTip',
          //     seriesIndex: 0, //第几条series
          //     dataIndex: index, //第几个tooltip
          // });
          // 高亮指定的散点
          that.chart.dispatchAction({
            type: "highlight",
            seriesIndex: 0, //第几条series
            dataIndex: index, //第几个tooltip
          });
        }
      });

      window.addEventListener("resize", function () {
        that.chart.resize();
      });
    },
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
    }
  },
};
</script>
<style scoped>
.container {
  width: 900px;
  height: 630px;
}
</style>

<think>好的,用户的问题是关于如何在高德地图中实现类似ECharts的数据可视化效果,具体来说是如何通过高德地图API集成ECharts或者实现类似的图表与数据展示效果。首先,我需要确认用户的需求是什么。他们可能已经使用过高德地图,现在希望在地图上展示更丰富的数据可视化图表,比如图、线图或者热力图等。 接下来,我要回顾已有的知识库中的引用内容。引用[1]和[2]提到了echarts-extension-amap这个项目,它是ECharts的扩展,允许在高德地图上展示各种图表。引用[3]则是一个关于在Vue3中使用ECharts图和高德地图的教程,可能涉及API的获取和使用步骤。 用户的需求是集成ECharts到高德地图中,或者使用高德地图API实现类似效果。根据引用内容,最直接的解决方案应该是推荐使用echarts-extension-amap这个扩展,因为它专门为ECharts和高德地图的集成设计,可以简化开发流程。此外,可能还需要指导用户如何获取高德地图的API key,以及如何在项目中安装和配置这个扩展。 我需要分步骤来回答,首先介绍使用echarts-extension-amap的方法,包括安装、初始化地图、配置图表以及集成数据层。然后,如果用户不想使用第三方扩展,可能需要另一种方法,即通过高德地图的自定义图层或覆盖物来实现基本的数据可视化,比如手动绘制图或热力图。不过这种方法可能较为复杂,需要更多的编码工作。 同时,用户可能关心性能优化和常见问题,比如如何解决地图加载问题、数据量过大时的性能优化方法,以及如何自定义样式。这些都需要在回答中提到,确保用户能够顺利实现需求。 最后,根据用户的问题生成相关问题,可能涉及如何自定义图表样式、性能优化、动态数据更新以及与其他地图服务的比较等。这些问题可以帮助用户进一步探索和解决可能遇到的挑战。</think>### 分步实现高德地图ECharts集成的方法 #### 方法一:使用官方扩展库`echarts-extension-amap` 1. **环境准备** - 安装依赖: ```bash npm install echarts @amap/amap-jsapi-loader echarts-extension-amap ``` - 获取高德地图API Key: 访问[高德开放平台](https://lbs.amap.com/),注册账号后创建应用,获取Web端JS API的Key[^3]。 2. **地图初始化** ```javascript import * as echarts from 'echarts'; import 'echarts-extension-amap'; // 引入扩展 // 初始化地图容器 const chart = echarts.init(document.getElementById('map-container')); chart.setOption({ amap: { viewMode: '3D', // 支持2D/3D模式 center: [116.3974, 39.9093], // 北京坐标 zoom: 10, mapStyle: 'amap://styles/light', // 地图样式 apiKey: 'YOUR_AMAP_API_KEY' // 替换为实际Key[^3] }, series: [] // 数据序列将在下一步添加 }); ``` 3. **数据可视化集成** ```javascript // 添加图层 chart.setOption({ series: [{ type: 'scatter', coordinateSystem: 'amap', // 指定坐标系为高德地图 data: [[116.3974, 39.9093, 100], [121.4737, 31.2304, 200]] // [经度, 纬度, 值] }] }); ``` #### 方法二:手动实现基础集成(无第三方扩展) 1. **地图ECharts双容器叠加** ```html <div id="map-container" style="width: 100%;height:600px;"> <div id="echarts-overlay" style="position:absolute;pointer-events:none;"></div> </div> ``` 2. **坐标转换** ```javascript // 将地理坐标转换为屏幕坐标 function lnglatToPixel(lng, lat) { const projection = map.getProjection(); return projection.lngLatToContainer(new AMap.LngLat(lng, lat)); } ``` 3. **动态更新图表位置** ```javascript map.on('movend', () => { const newData = rawData.map(item => { const pixel = lnglatToPixel(item[0], item[1]); return [pixel.getX(), pixel.getY(), item[2]]; }); echartInstance.setOption({ series: [{ data: newData }] }); }); ``` ### 性能优化建议 1. **数据抽稀**: 对大规模数据集使用`LOD`(Levels of Detail)技术,根据缩放级别动态调整数据精度[^1]。 2. **WebGL加速**: 使用`echarts-gl`库实现WebGL渲染: ```javascript import 'echarts-gl'; series: [{ type: 'scatterGL', // 使用WebGL版本 coordinateSystem: 'amap', data: [...] }] ``` 3. **图层分治**: 将静态数据(如行政区划)与动态数据(如实时轨迹)分离到不同图层,单独控制刷新频率[^2]。 ### 常见问题解决 1. **地图加载异常**: - 检查API Key是否绑定`Web端(JS API)`服务 - 确认网络协议匹配(HTTPS环境需使用HTTPS API) 2. **坐标偏移问题**: 使用高德官方坐标转换接口: ```javascript AMap.convertFrom([116.312,39.978], 'gps', (status, result) => { if (status === 'complete') { console.log(result.locations); // 转换后坐标 } }); ``` ### 效果增强方案 ```javascript // 热力图示例 chart.setOption({ series: [{ type: 'heatmap', coordinateSystem: 'amap', blurSize: 30, data: [[116.40, 39.91, 100], ...] }] }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值