vue中使用echarts,顯示某個省的地圖,且根據後臺返回經緯度在地圖上顯示自定義圖標

實現效果:

如上所示,顯示的是貴州省的地圖,且分爲三種圖標,表示的是三種不同類型,根據經緯度顯示具體位置。

 

實現代碼:

1.html:

<div id="distributionDiv"></div>

2.js:

import vueEcharts from 'echarts';
import zaixian from "@/assets/images/homeIcon/zaixian.png";
import shangxian from "@/assets/images/homeIcon/shangxian.png";
import lixian from "@/assets/images/homeIcon/lixian.png";
// 註冊貴州省地圖
const guizhouJson = require("@/assets/json/guizhou.json");
vueEcharts.registerMap("guizhou", guizhouJson);  

引入了三個圖片,引入了貴州省的一個json文件,這個自己百度即可,使用json文件註冊貴州省地圖。

3.options:

getDistributionOptions(){
  // 獲取經緯度數據
  const seriesList = [
    {
      image: zaixian,
      data: [
        {value: [106.9, 27.7]},
        {value: [105.29, 27.32]},
        {value: [106.04,27.03]},
        {value: [104.82,26.58]},
      ]
    },
    {
      image: shangxian,
      data: [
        {
          value: [107.43,28.56]
        },
      ]
    },
    {
      image: lixian,
      data: [
        {
          value: [107.5,27.76]
        }
      ]
    }
  ];
  // 自定義圖標
  const series = seriesList.map((v) => {
    return {
      type: "custom", //配置顯示方式爲用戶自定義
      coordinateSystem: "geo",
      renderItem(params, api) {
        //具體實現自定義圖標的方法
        return {
          type: "image",
          style: {
            image: v.image,
            x: api.coord([
              v.data[params.dataIndex].value[0],
              v.data[params.dataIndex].value[1]
            ])[0],
            y: api.coord([
              v.data[params.dataIndex].value[0],
              v.data[params.dataIndex].value[1]
            ])[1],
            width: '29',
            height: '35',
          }
        };
      },
      data: v.data,
    };
  });
  // options
  const distributionOptions = {
    tooltip: {
      show: true,
      trigger: "item",
      triggerOn: "click",
      formatter: "名稱:{b}<br />座標:{c}"
    },
    series,
    geo: {
      //引入貴州省的地圖
      map: "guizhou",
      label: {
        emphasis: {
          show: true
        }
      },
      layoutCenter: ["50%", "50%"], //設置後left/right/top/bottom等屬性無效
      layoutSize: "90%",
      roam: true, //開啓鼠標縮放和漫
      zoom: 2,
      label: {
        normal: { //靜態的時候展示樣式
          show: true, //是否顯示地圖省份得名稱
          textStyle: {
            color: "#fff",
            // fontSize: 10,
            fontFamily: "Arial"
          }
        },
        emphasis: { //動態展示的樣式
          color:'#fff',
        },
      },
      itemStyle: {
        normal: {
          borderColor: "#07919e",
          areaColor: '#1c2f59',
          textStyle: {
            color: "#fff"
          },
          shadowBlur: 10,
          shadowOffsetX: 10,
        },
        emphasis: {
          areaColor: "#1c2f59",
          color: "#fff"
        },
      },
    }
  };
  var myChart = vueEcharts.init(document.getElementById('distributionDiv'));
  myChart.setOption(distributionOptions);
  window.onresize = function(){
    myChart.resize();
  }
}, 

包含三步:獲取經緯度數據,可以到時候請求後臺數據;顯示自定義圖標;options配置。然後獲取到對應的div元素,給他設置option,設置自適應。

4.mounted:

mounted(){
  this.getDistributionOptions();
},

一進來就執行該方法,加載地圖。

 

到現在基本上就可以了。

 

參考鏈接:

https://blog.csdn.net/weixin_30381793/article/details/99930705

https://blog.csdn.net/qq_42036203/article/details/103529566

https://www.jianshu.com/p/d6e889af6516(包含所有省市的json文件)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章