
1.html中引入
<script src='../../../static/plugins/amap/aMapPolyline.js'></script>
<script src="https://webapi.amap.com/maps?key=地图的key值&v=1.4.13&plugin=Map3D,AMap.DistrictSearch,Loca,AMap.DistrictLayer,SimpleMarker,DistrictExplorer,"></script>
<script src="https://webapi.amap.com/loca?v=1.3.2&key=地图的key值"></script>
<script src="//webapi.amap.com/ui/1.0/main.js"></script>
2. 新建vue文件 命名investMap.vue 代码如下:
<template>
<div style="position:relative;width:100%;height:100%">
<div id="mapContainer"></div>
<div class="mapRange">
<div class="mapRange-item">排名情况分布</div>
<div
class="mapRange-item"
v-for="(item, i) in rightColor"
:key="`colorList-${i}`"
>
<i :style="{ background: item.color }"></i>
{{ item.min ? item.min + '~' : item.min == 0 ? '0~' : '大于'
}}{{ item.max }}
</div>
</div>
</div>
</template>
<script>
import { citysMap } from '@common/constant'
let that
export default {
data() {
return {
countyData: [],
districtExplorer: null,
textOverlayGroup: new AMap.OverlayGroup([]),
filterText: [
'衢江区',
'拱墅区',
'上城区',
'下城区',
'江干区',
'滨江区',
'江北区',
'鄞州区',
'南湖区',
'瓯海区',
'金东区'
],
adcodes: {
330100: '杭州市',
330200: '宁波市',
330300: '温州市',
330400: '嘉兴市',
330500: '湖州市',
330600: '绍兴市',
330700: '金华市',
330800: '衢州市',
330900: '舟山市',
331000: '台州市',
331100: '丽水市'
},
citys: citysMap,
renderList: null,
districtExplorer: null,
colorList: [],
rightColor: [],
lv: '',
unit: ''
}
},
beforeMount() {
that = this
},
mounted() {
this.initMap()
this.getRightColor()
},
methods: {
getRightColor(type, colorData) {
const data = {
type: type
}
this.rightColor = colorData
},
initMap() {
this.map = new AMap.Map('mapContainer', {
mapStyle: '',
zoom: 8,
center: [120.012427, 29.203573],
enableMapClick: false
})
},
mapCountyChange(args, list, level, unit) {
const items = args
this.countyData = list
this.dataList = JSON.parse(JSON.stringify(list))
const arr = items.map(v => {
return v.adcode
})
if (arr.length == 0 || arr[0] == 'zj') {
this.renderList = Object.keys(this.adcodes)
} else {
this.renderList = arr
}
this.lv = level
this.unit = unit
this.countryRender(true, false, level)
},
countryRender(clear = false, defaultStyle = false, level) {
const that = this
AMapUI.loadUI(['geo/DistrictExplorer'], function(DistrictExplorer) {
let districtExplorer = null
if (!that.districtExplorer) {
that.districtExplorer = new DistrictExplorer({
eventSupport: true,
map: that.map
})
}
districtExplorer = that.districtExplorer
var tipMarker = null
if (!that.tipMarker) {
that.tipMarker = new AMap.Marker({
bubble: true,
offset: new AMap.Pixel(0, 0)
})
}
tipMarker = that.tipMarker
districtExplorer.on('featureMouseout featureMouseover', function(
e,
feature
) {
var isHover = e.type === 'featureMouseover'
if (!isHover) {
tipMarker.setMap(null)
return
}
tipMarker.setMap(that.map)
tipMarker.setPosition(e.originalEvent.lnglat)
const positionName = that.getCountyDetailData(
feature.properties.name,
that.lv
)
console.log('positionName', positionName)
const unit = that.unit
const div =
positionName.rate == null || positionName.rate == 0
? `<div class="label-value"><b>${
positionName.value
}${unit}</b></div><div class="rateMarker"><span class="zColor">${
positionName.rate == null ? '' : '+' + positionName.rate + '%'
}</span></div>`
: positionName.rate > 0
? `<div class="label-value"><b>${
positionName.value
}${unit}</b></div><div class="rateMarker"><i class="hot" ></i><span class='rColor'>${
positionName.rate == null ? '' : '+' + positionName.rate + '%'
}</span></div>`
: `<div class="label-value"><b>${
positionName.value
}${unit}</b></div><div class="rateMarker"><i class="low" ></i><span color='gColor'>${
positionName.rate == null ? '' : positionName.rate + '%'
}</span></div>`
tipMarker.setLabel({
offset: new AMap.Pixel(20, 20),
content:
'<span class="rank-radiu">' +
positionName.orderBy +
'</span>' +
feature.properties.name +
div
})
})
districtExplorer.on('featureClick', function(e, feature) {
that.countyName = feature.properties.name
})
function renderAreaNode(areaNode, level) {
districtExplorer.renderSubFeatures(areaNode, function(feature, i) {
if (level == 2) {
if (
that.filterText.filter(v => feature.properties.name == v)
.length == 0
) {
const text = new AMap.Text({
map: that.map,
position: feature.properties.center,
text: feature.properties.name,
zIndex: 100,
color: 'black'
})
that.textOverlayGroup.addOverlay(text)
}
} else if (level == 1) {
const text = new AMap.Text({
map: that.map,
position: feature.properties.center,
text: feature.properties.name,
zIndex: 100,
color: 'black'
})
that.textOverlayGroup.addOverlay(text)
}
const name = feature.properties.name
let color = null
let strokeColor = '#ccc'
const d = that.getCountyDetailData(name, level)
if (d) {
color = d.color
}
strokeColor = '#D5D4D4'
return {
cursor: 'default',
bubble: true,
strokeColor: '#D5D4D4',
strokeOpacity: 1,
strokeWeight: 1,
fillColor: color,
fillOpacity: 1
}
})
}
if (clear) {
districtExplorer.clearFeaturePolygons()
that.textOverlayGroup.setMap(null)
that.textOverlayGroup.clearOverlays()
}
let adcodes
if (level == 2) {
adcodes = that.renderList
} else if (level == 1) {
adcodes = ['330000']
}
districtExplorer.loadMultiAreaNodes(adcodes, function(
error,
areaNodes
) {
districtExplorer.setAreaNodesForLocating(areaNodes)
if (areaNodes.length > 0) {
for (var i = 0, len = areaNodes.length; i < len; i++) {
renderAreaNode(areaNodes[i], level)
}
} else {
}
})
})
},
getCountyDetailData(name, level) {
if (level == 2) {
const t = this.countyData.filter(v => {
return v.county.indexOf(name.substr(0, 2)) > -1
})
return t[0]
} else if (level == 1) {
const t = this.countyData.filter(v => {
return v.city == name
})
return t[0]
}
},
getColor(value) {
if (this.rightColor.length > 0) {
const c = this.rightColor.find(v => {
return v.min <= value && v.max >= value
})
return c ? c.color : '#FFFFFF'
} else {
return '#FFFFFF'
}
}
}
}
</script>
3.样式
<style lang="less">
#mapContainer {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
.amap-overlay-text-container {
background: none;
border: none;
color: black;
}
.amap-info-content {
padding: 0;
background-color: transparent;
font-weight: bold;
}
.investment-right-map-content {
.amap-info-content {
color: black !important;
}
.amap-maps {
color: black;
}
}
}
.mapRange {
position: absolute;
right: 0.1rem;
bottom: 0.3rem;
width: 1rem;
color: white;
padding-left: 0.2rem;
background-color: rgba(3, 44, 88, 0.43);
min-width: 95px;
&-item {
line-height: 0.35rem;
height: 0.35rem;
i {
display: inline-block;
width: 0.12rem;
height: 0.12rem;
}
}
}
.amap-logo {
display: none !important;
}
.amap-marker-label {
position: absolute;
padding: 10px;
border: 0 none;
background-color: rgba(0, 0, 0, 0.65);
color: white;
white-space: nowrap;
box-shadow: 0 0 5px 0 rgba(0, 0, 0, 0.3);
border-radius: 2px;
height: 65px;
min-width: 90px;
}
.amap-marker-label:after {
position: absolute;
border: 5px solid transparent;
border-right-color: rgba(0, 0, 0, 0.65);
top: 30px;
left: -8%;
content: '';
width: 0;
height: 0;
}
.label-value {
margin-top: 10px;
b {
font-weight: bold;
font-size: 14px;
font-family: DINAlternate-Bold;
}
i {
margin-left: 5px;
}
}
.rank-radiu {
color: white;
display: inline-block;
width: 15px;
height: 15px;
background: rgba(42, 107, 255, 0.5);
margin-right: 5px;
padding: 3px;
border-radius: 50%;
text-align: center;
vertical-align: middle;
line-height: 15px;
}
.rateMarker {
margin-top: 5px;
margin-bottom: 4px;
.zColor {
color: #32c5ff;
}
.gColor {
color: #44d7b6;
}
.rColor {
color: #ff4640;
}
.hot {
display: inline-block;
width: 18px;
width: 13px;
height: 19px;
background: url('~@assets/images/map-icons/up.png') no-repeat center center;
background-size: 100% 100%;
margin-right: 5px;
}
.low {
display: inline-block;
color: #fa6400;
width: 13px;
height: 19px;
color: #0091ff;
margin-right: 5px;
background: url('~@assets/images/map-icons/down.png') no-repeat center
center;
background-size: 100% 100%;
}
}
</style>
4. citysMap 的内容
export const citysMap = [
{
name: '浙江省',
adcode: 'zj',
type: 1,
active: true
},
{
name: '杭州市',
adcode: '330100',
type: 1,
active: false
},
{
name: '宁波市',
adcode: '330200',
type: 1,
active: false
},
{
name: '温州市',
adcode: '330300',
type: 1,
active: false
},
{
name: '嘉兴市',
adcode: '330400',
type: 1,
active: false
},
{
name: '湖州市',
adcode: '330500',
type: 1,
active: false
},
{
name: '绍兴市',
adcode: '330600',
type: 1,
active: false
},
{
name: '金华市',
adcode: '330700',
type: 1,
active: false
},
{
name: '衢州市',
adcode: '330800',
type: 1,
active: false
},
{
name: '舟山市',
adcode: '330900',
type: 1,
active: false
},
{
name: '台州市',
adcode: '331000',
type: 1,
active: false
},
{
name: '丽水市',
adcode: '331100',
type: 1,
active: false
}
]