效果图
代码
<!--
* @Author: DZM
* @Date: 2022-03-16 18:25:07
* @LastEditors: tianwang
* @LastEditTime: 2022-03-16 18:57:13
* @Description:
-->
<template lang="">
<div>
<div ref="map" class="map"></div>
<el-switch
v-model="areaLineSwitch"
@change="areaLineChange"
active-text="开启"
inactive-text="关闭">
</el-switch>
</div>
</template>
<script>
export default {
data() {
return {
areaLineSwitch: false,
adcode: '110000',
map: null,
districtExplorer: null,
zoom: 8,
center: [116.407387, 39.904179]
}
},
methods: {
areaLineChange(val) {
this.areaLineSwitch = val;
if (val) {
this.drawAreaNode();
} else {
this.districtExplorer && this.districtExplorer.clearFeaturePolygons();
}
},
initMap() {
const map = new AMap.Map(this.$refs.map, {
mapStyle: 'dark',
zoom: this.zoom,
center: this.center,
features: ['bg', 'point', 'road'],
resizeEnable: true,
expandZoomRange: true
});
return map
},
initDistrictExplorer() {
const { map } = this;
return new Promise((resolve, reject) => {
AMapUI.loadUI(
["geo/DistrictExplorer"],
DistrictExplorer => {
resolve(new DistrictExplorer({ map, eventSupport: true }));
}
)
});
},
drawAreaNode() {
const { districtExplorer, adcode } = this;
districtExplorer.loadAreaNode(adcode, (error, areaNode) => {
if (error) {
console.error(error);
return;
}
const colors = [
"#F8E71C",
"#00FFC6",
"#5DFF00",
"#EB2F96",
"#FF6425",
"#844AFF",
"#00A8FF",
"#F81C1C",
"#00DBFF",
"#D5FF39",
"#87E8DE",
"#FF85C0",
"#52C41A",
"#FAAD14",
"#FFD8BF",
"#D9F7BE"
];
const colorLength = colors.length;
districtExplorer.renderSubFeatures(areaNode, (feature, i) => {
const fillColor = colors[i % colorLength];
return {
cursor: "default",
bubble: true,
strokeColor: "#fff",
strokeWeight: 1,
strokeOpacity: 1,
fillColor,
fillOpacity: 0.2
};
});
});
},
},
mounted() {
this.map = this.initMap();
this.initDistrictExplorer().then((districtExplorer) => {
this.districtExplorer = districtExplorer;
});
}
}
</script>
<style scoped>
.map {
width: 100%;
height: calc(80vh);
}
</style>