省市區(qū)3級(jí)下鉆
示意圖如下
<template>
<div class="home-container">
<el-button type="primary" @click="mapBack">返回上級(jí)地圖</el-button>
<div id="map"></div>
</div>
</template>
<script lang="js">
import {defineComponent} from 'vue';
import * as echarts from 'echarts';
export default defineComponent({
name: 'map',
data() {
return {
echartsMap: null,
mapJson: {},
level: 'province'
}
},
props: {
markers: {
type: Array,
default: () => []
}
},
watch: {
level(val) {
console.log(val)
}
},
mounted() {
this.loadMapData('320000')
},
methods: {
mapBack() {
if (this.level === 'district') {
this.level = 'city'
const parentCode = this.mapJson.features[0].properties.acroutes[2]
this.loadMapData(parentCode)
} else if (this.level === 'city') {
this.level = 'province'
this.loadMapData('320000')
}
},
loadMapData(areaCode) {
// eslint-disable-next-line no-undef
AMapUI.loadUI(['geo/DistrictExplorer'], DistrictExplorer => {
//創(chuàng)建一個(gè)實(shí)例
var districtExplorer = window.districtExplorer = new DistrictExplorer({
eventSupport: true, //打開事件支持
map: this.map
});
districtExplorer.loadAreaNode(areaCode, (error, areaNode) => {
if (error) {
console.error(error);
return;
}
let mapJson = {};
mapJson.type = 'FeatureCollection'
//特別注意這里哦馋吗,如果查看過正常的geojson文件撑教,都會(huì)發(fā)現(xiàn)只搁,文件都是以features 字段開頭的豆挽,所以這里要記得加上
mapJson.features = areaNode.getSubFeatures();
this.mapJson = mapJson
this.loadMap('province', mapJson);
});
});
},
//通過loadMap函數(shù)加載地圖
loadMap(mapName, data) {
if (this.echartsMap) this.echartsMap.dispose();
//注冊(cè)并賦值給echartsMap
this.echartsMap = echarts.init(document.getElementById('map'));
if (data) {
echarts.registerMap(mapName, data);//把geoJson數(shù)據(jù)注入echarts
//配置echarts的option
const option = {
backgroundColor: '#020933',
geo: {
map: mapName,
aspectScale: 0.75, //長(zhǎng)寬比
zoom: 1.1,
roam: false,
label: {
normal: {
show: true,
textStyle: {
color: '#fff'
}
},
emphasis: {
textStyle: {
color: '#fff'
}
}
},
nameProperty: 'name',
data: this.markers,
itemStyle: {
normal: {
borderColor: '#2ab8ff',
borderWidth: 1.5,
areaColor: '#12235c'
},
emphasis: {
areaColor: '#2AB8FF',
borderWidth: 0,
color: 'green'
}
},
},
series: [
{
type: 'effectScatter',
coordinateSystem: 'geo',
showEffectOn: 'render',
rippleEffect: {
period: 15,
scale: 4,
brushType: 'fill'
},
hoverAnimation: true,
itemStyle: {
normal: {
color: '#ffff',
shadowBlur: 10,
shadowColor: '#333'
}
},
data: this.markers
}
]
};
this.echartsMap.setOption(option);
this.echartsMap.on('click', this.echartsMapClick);
}
},
echartsMapClick(params) {
const currentMap = this.mapJson.features.filter(item => {
if (item.properties.name === params.name) return true
})
this.level = currentMap[0].properties.level
if (this.level === 'district') {
let mapJson = {};
mapJson.type = 'FeatureCollection'
mapJson.features = currentMap
this.mapJson = mapJson
}
this.$emit('mapClick', {level: this.level, code: currentMap[0].properties.adcode})
},
},
beforeUnmount() {
this.echartsMap = null
}
});
</script>
<style scoped>
#map {
height: 600px;
width: 800px;
}
</style>