引入、注册、实例化地图数据
const municipalMap = require(`./json/map.json`)
echarts.registerMap('惠州市', municipalMap)
this.mapChart = echarts.getInstanceByDom(document.getElementById('mapChart'))
if (this.mapChart == null) {
this.mapChart = echarts.init(document.getElementById('mapChart'))
}
添加地图marker数据
let iconData = [
{ type: '一般', name: '博罗县', value: [114.32074, 23.302638] },
{ type: '一般', name: '博罗县', value: [114.30074, 23.382638] },
{ type: '重大', name: '龙门县', value: [114.30497, 23.68495] },
{ type: '重大', name: '惠城区', value: [114.545898, 23.160987] },
{ type: '紧急', name: '惠阳区', value: [114.401134, 22.92713] },
{ type: '其他', name: '大亚湾区', value: [114.555124, 22.762723] },
{ type: '其他', name: '惠东县', value: [114.9402, 23.122134] },
{ type: '一般', name: '仲恺高新区', value: [114.251124, 23.075723] }
]
let prefix = 'image://'
iconData.forEach((item) => {
if (item.type === '一般') {
item.symbol = prefix + this.ybIcon
} else if (item.type === '重大') {
item.symbol = prefix + this.zdIcon
} else if (item.type === '紧急') {
item.symbol = prefix + this.jjIcon
} else {
item.symbol = prefix + this.qtIcon
}
})
地图主要配置项
geo: [
{
map: '惠州市',
roam: false,
zoom: 1.26,
zlevel: 2,
z: 1,
aspectScale: 1.05,
layoutSize: '80%',
layoutCenter: ['52%', '48%'],
label: {
show: true,
color: '#fff',
fontWeight: 700,
fontSize: this.nowSize(12)
},
emphasis: {
disabled: false,
label: {
show: true,
color: '#fff',
fontWeight: 700,
fontSize: this.nowSize(12)
},
itemStyle: {
borderWidth: this.nowSize(2),
shadowColor: '#003585',
focus: 'none',
areaColor: 'orange'
}
},
itemStyle: {
borderColor: '#003585',
borderWidth: this.nowSize(1),
shadowColor: '#003585',
shadowOffsetX: this.nowSize(2),
shadowOffsetY: this.nowSize(3),
shadowBlur: 6,
areaColor: '#228FF4'
},
selectedMode: 'single',
select: {
label: {
show: true,
color: '#fff',
fontWeight: 700,
fontSize: this.nowSize(12)
},
itemStyle: {
borderWidth: this.nowSize(2),
shadowColor: '#003585',
focus: 'none',
areaColor: 'orange'
}
}
},
{
map: '惠州市',
roam: false,
zlevel: -1,
zoom: 1.26,
z: 1,
aspectScale: 1.05,
layoutCenter: ['52%', '49%'],
layoutSize: '80%',
itemStyle: {
borderWidth: this.nowSize(0),
borderColor: 'transpercent',
shadowColor: '#086489',
shadowOffsetX: 0,
shadowOffsetY: this.nowSize(10),
shadowBlur: this.nowSize(10),
areaColor: '#06336C'
}
}
],
地图点击态实现主要代码
selectedMode: 'single',
select: {
label: {
show: true,
color: '#fff',
fontWeight: 700,
fontSize: this.nowSize(12)
},
itemStyle: {
borderWidth: this.nowSize(2),
shadowColor: '#003585',
focus: 'none',
areaColor: 'orange'
}
}
添加区域中心点
{
type: 'effectScatter',
coordinateSystem: 'geo',
silent: true,
geoIndex: 0,
encode: {
value: 2
},
data: [
{ name: '博罗县', value: [114.39074, 23.339638, 10] },
{ name: '龙门县', value: [114.194197, 23.66548, 10] },
{ name: '惠城区', value: [114.605898, 23.198987, 10] },
{ name: '惠阳区', value: [114.491124, 22.877723, 10] },
{ name: '大亚湾区', value: [114.497124, 22.710723, 10] },
{ name: '惠东县', value: [115.010202, 23.082134, 10] },
{ name: '仲恺高新区', value: [114.311124, 23.033723, 10] }
],
symbolSize: this.nowSize(10),
showEffectOn: 'render',
rippleEffect: {
brushType: 'stroke'
},
label: {
show: false
},
itemStyle: {
shadowBlur: this.nowSize(10),
shadowColor: '#333',
color: '#19E3FF'
},
emphasis: {
scale: true
},
zlevel: 2
}
添加点击地图区域时的引导线
this.mapChart.setOption(mapOption)
let that = this
this.mapChart.on('click', (params) => {
if (that.popupTitle === params.name) {
this.hideLine()
} else {
that.popupTitle = params.name
var points = []
if (params.name === '博罗县') {
points = [
[114.39074, 23.339638],
[114.70074, 23.652638],
[114.74074, 23.652638]
]
} else if (params.name === '龙门县') {
points = [
[114.194197, 23.66548],
[114.684197, 23.66548],
[114.724197, 23.62548]
]
} else if (params.name === '惠城区') {
points = [
[114.605898, 23.198987],
[114.725898, 23.608987],
[114.765898, 23.608987]
]
} else if (params.name === '惠阳区') {
points = [
[114.491124, 22.877723],
[114.081124, 22.757723],
[114.081124, 22.727723]
]
} else if (params.name === '大亚湾区') {
points = [
[114.497124, 22.710723],
[114.417124, 22.520723],
[114.387124, 22.520723]
]
} else if (params.name === '惠东县') {
points = [
[115.010202, 23.082134],
[115.150202, 23.422134],
[115.150202, 23.462134]
]
} else if (params.name === '仲恺高新区') {
points = [
[114.311124, 23.033723],
[114.051124, 22.783723],
[114.051124, 22.733723]
]
}
that.mapChart.setOption({
series: [
{
type: 'lines',
coordinateSystem: 'geo',
zlevel: 3,
silent: true,
polyline: true,
lineStyle: {
color: '#fff'
},
data: [
{
coords: points
}
]
}
]
})
}
})
取消地图选中和引导线
hideLine() {
if (this.mapChart) {
this.mapChart.dispatchAction({
type: 'geoUnSelect',
name: this.popupTitle
})
}
this.popupTitle = ''
this.mapChart.setOption({
series: [
{
zlevel: 3,
type: 'lines',
data: []
}
]
})
}
防止重复触发地图点击事件,导致地图初始化时点击地图选中触发两次以上,导致点击态消失(顺便加一个接口获取地图数据并初始化地图,初始化时要调用一下取消选中方法)
this.mapLoading = true
getMapInfo(data)
.then((res) => {
this.mapLoading = false
console.log('地图数据----', res)
this.mapData = res.data
if (this.mapChart) {
this.mapChart.off('click')
}
this.$nextTick(() => {
this.initMap()
this.hideLine()
})
})
.catch(() => {
this.mapLoading = false
})
离开页面销毁地图实例
destroyed() {
if (this.mapChart) {
this.mapChart.dispose()
}
},