<template>
<div id="app">
<div class="header" id="header"></div>
</div>
</template>
<script>
import echarts from "echarts";
import axios from "axios";
export default {
name: "App",
methods: {
async initChart() {
let chart = echarts.init(document.getElementById("header"));
let { data } = await this.getAllJson();
let chinaGeoJson = await this.getGeoJson("100000_full.json");
this.initEcharts(chinaGeoJson.data, "全国", chart, data);
},
initEcharts(geoJson, name, chart, alladcode) {
echarts.registerMap(name, geoJson);
let option = {
title: {
text: name,
left: "center",
},
series: [
{
type: "map",
map: name,
itemStyle: {
areaColor: "#1890ff",
},
},
],
};
chart.setOption(option);
chart.off("click");
chart.on("click", (params) => {
let clickRegionCode = alladcode.filter(
(areaJson) => areaJson.name === params.name
)[0].adcode;
console.log(params, "params", clickRegionCode);
this.getGeoJsona(110100)
.then((regionGeoJson) =>
this.initEcharts(regionGeoJson.data, params.name, chart, alladcode)
)
.catch((err) => {
console.log(err);
this.getGeoJson().then((chinaGeoJson) =>
this.initEcharts(chinaGeoJson.data, "全国", chart, alladcode)
);
});
});
},
initmap() {
this.initChart();
},
async getGeoJson() {
return await axios.get(`/map/${100000}_full.json`);
},
async getGeoJsona(code) {
return await axios.get(`/map/city/${code}.json`);
},
async getAllJson() {
return await axios.get(`/map/all.json`);
},
},
async mounted() {
this.initChart();
},
};
</script>
<style scoped>
.header {
height: 500px;
width: 500px;
background-color: #f2f2f2;
}
</style>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>地图下钻</title>
<style>
#main {
height: 100vh;
width: 100vw;
}
</style>
</head>
<body>
<div id="main"></div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/echarts/4.7.0/echarts.min.js"></script>
<script>
let publicUrl = 'https://geo.datav.aliyun.com/areas_v2/bound/';
async function initChart() {
let chart = echarts.init(document.getElementById('main'));
let alladcode = await getGeoJson('all.json')
let chinaGeoJson = await getGeoJson('100000_full.json')
initEcharts(chinaGeoJson, '全国', chart, alladcode)
}
initChart();
function initEcharts(geoJson, name, chart, alladcode) {
echarts.registerMap(name, geoJson);
let option = {
title: {
text: name,
left: 'center'
},
series: [{
type: 'map',
map: name,
itemStyle: {
areaColor: '#1890ff'
}
}]
}
chart.setOption(option)
chart.off("click");
chart.on('click', params => {
let clickRegionCode = alladcode.filter(areaJson => areaJson.name === params.name)[0].adcode;
getGeoJson(clickRegionCode + '_full.json').then(regionGeoJson => initEcharts(regionGeoJson, params.name, chart, alladcode))
.catch(err => {
getGeoJson('100000_full.json').then(
chinaGeoJson=>initEcharts(chinaGeoJson, '全国', chart, alladcode)
)
})
})
}
async function getGeoJson(jsonName) {
return await $.get(publicUrl + jsonName)
}
</script>
</body>
</html>