接下来写一个获取静态资源的函数 //注意文件存放位置换成自己的
引入cityMap
import { cityMap } from '../map/china-main-city-map.js';
const getAssetsFile = (chinaId = 100000) => {
return axios({
method: 'get',
url: new URL(`../map/${chinaId}.json`, import.meta.url).href,
})
.then((res) => {
return res;
})
.catch((err) => {
console.log('地图加载失败==========', err);
});
};
初始化echarts // 地图初始化 ,获取本地地图文件并开始绘制
const plantOtherInfo=ref({
adCode:100000
})
const mapInitName=ref('china')
const myChart = ref(null);
const initMapChart = async () => {
const joinName = cityMap[plantOtherInfo.value.adCode];
const response = await getAssetsFile(plantOtherInfo.value.adCode);
mapInitName.value = joinName;
myChart.value = echarts.init(document.getElementById('mapCharts'));
initOptionsData(myChart.value, response);
};
绘制函数
const initOptionsData = (myChart, mapJson) => {
echarts.registerMap(mapInitName.value, mapJson);
myChart.setOption({
tooltip: {
trigger: 'item',
show: false,
},
geo: [
{
map: mapInitName.value,
aspectScale: 1,
zoom: 1,
layoutCenter: ['50%', '50%'],
layoutSize: '100%',
show: true,
roam: false,
label: {
show: true,
color: '#fff',
},
itemStyle: {
areaColor: {
image: mapBg,
repeat: 'no-repeat',
},
borderColor: 'rgba(218, 241, 243)',
borderWidth: 1.5,
shadowColor: 'rgba(218, 241, 243)',
},
emphasis: {
itemStyle: {
show: false,
color: '#fff',
areaColor: '#80899C',
},
label: {
show: true,
color: '#fff',
},
},
},
{
type: 'map',
map: mapInitName.value,
zlevel: -1,
aspectScale: 1,
zoom: 1,
layoutCenter: ['50%', '51.5%'],
layoutSize: '100%',
roam: false,
silent: true,
itemStyle: {
borderWidth: 1,
shadowOffsetY: 5,
shadowBlur: 15,
borderColor: 'rgba(105, 135, 139)',
shadowColor: 'rgba(105, 135, 139)',
areaColor: {
image: mapBg,
repeat: 'no-repeat',
},
},
regions: [
{
name: '南海诸岛',
itemStyle: {
normal: {
opacity: 0,
},
},
label: {
show: false,
},
},
],
},
],
});
myChart.on('click', (params) => {
});
};