Vue3.x使用Echarts绘制世界地图并进行定点
一、需求
- 绘制世界地图并根据返回经纬度数据进行定点
- 将定点数据展示在世界地图内
二、解决
- 绘制世界地图,利用
Echarts
图表组件时间,需要世界地图Geojson数据的可以在资源中下载世界地图Geojson数据-Javascript文档类资源-CSDN文库,世界地图Geojson
文件存储在前端本地的静态资源文件夹下,代码如下:
<template>
<div>
...
<div id="worldgraph"></div>
</div>
</template>
<script setup>
import * as echarts from 'echarts';
import worldData from '@/assets/json/world.json';
import { ref, onMounted, onUnmounted } from 'vue';
const worldGraph = ref(null);
onMounted(() => {
initData()
})
onUnmounted(() => {
if (worldGraph.value) {
worldGraph.value.dispose();
worldGraph.value = null;
}
});
const initData = () => {
if (worldGraph.value) {
worldGraph.value.dispose();
worldGraph.value = null;
}
const point = []
point.push({
name: '美国',
value: [-97.822, 37.751 + 15],
type: 'iconData'
})
drawnWorldChart(point)
};
const drawnWorldChart = (point) => {
if (!document.getElementById('ipprotrait-worldgraph')) {
return;
}
echarts.registerMap('world', JSON.stringify(worldData));
worldGraph.value = echarts.init(document.getElementById('ipprotrait-worldgraph'));
let option = {
grid: {
width: '100%',
height: '100%',
left: '0%',
right: '0%',
bottom: '0%',
containLabel: true
},
geo: {
type: 'map',
map: 'world',
aspectScale: 0.85,
emphasis: {
label: {
show: false
},
itemStyle: {
areaColor: '#F6FBFF',
borderColor: '#CEE0E7',
}
},
zoom: 1.2,
itemStyle: {
areaColor: '#F6FBFF',
borderColor: '#CEE0E7',
},
},
series: [
{
type: 'scatter',
coordinateSystem: 'geo',
symbolSize: 30,
symbol: 'image://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAKKADAAQAAAABAAAAKAAAAAB65masAAAE+ElEQVRYCbVYTWhUVxQ+b+a9mUk0hAhGDFiS2grusgpG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GXj5zkZg4/Al+BmPosFvDlRDqdw92PJ5oPXBXOyyqHT7SvwErcmGzfvmgfPepDE82luMCJqLGgyECBGGEes/eJc+ZMXrb5ft8E5Vrxg8NJpxWn4g6IChRYiR+3Eom5xNmzbgBfoJscluPMATgeCAYglECMtIT/Vz6KDw5asf37w/BqMYwlB7kUpxa13hhKIENA+DPOly+TmUx9ZQdlJZHJ5MlBrvUyzE+hBSqKeDxjdXQs2TjWojbGxDo6/kVNzUSJjSQQI/8HGybtjIy41t69ofMQyxjGKo7QkSHXYC0fEnxrlcsTqStXcpLw/R2xEgYMsYxhbC1XmH6kGawSOs4ovsL+Spw/H7iriSEWs2f8OK/yajp1CcRMLGH2hu1jx7x4X5+GdsVEHzHEqhgj0uyoSyDpkHBaYrGP+X+ftWfPpgy00UeMwm5ChDMYfw+GCxfxCoVvvJcvh5ZHRpKSy62EpVLSND6et3btmkKB/yAs15bg8MOW//P9Unr4sLDU3+/xYp82+rYkaVRSCOnyisW/CxMTZV7s0xaVZ0vxENSH75iiutBvVLL/APCb6OqC62Y+AAAAAElFTkSuQmCC',
label: {
show: false,
},
data: point,
}
]
};
worldGraph.value.setOption(option);
};
</script>
- 最后效果图如下: