创建地图
@observable mapStyle = 'midnight'
let map = new BMap.Map("demoMap",{
minZoom: 4,
maxZoom: 19,
enableMapClick: false
});
map.centerAndZoom(new BMap.Point(116.404, 39.915),this.mapZoom);
map.centerAndZoom('北京',this.mapZoom);
map.enableScrollWheelZoom(true);
map.enableDragging();
if(this.mapStyle && this.mapStyle.length > 10) {
map.setMapStyleV2({ styleId: this.mapStyle })
} else {
map.setMapStyle({ style: this.mapStyle })
}
if(this.mapStyle == 'special') {
map.setMapType(BMAP_HYBRID_MAP)
}
arr.forEach((item ,index)=> {
getBoundary(item)
})
function getBoundary(citys) {
let bdary = new BMap.Boundary();
bdary.get(citys.city, function(rs){
let count = rs.boundaries.length;
if(count > 0) {
for( var i = 0; i < count; i++ ){
let ply = new BMap.Polygon(rs.boundaries[i],{
strokeWeight: 1,
strokeColor: '#5dbac0',
strokeOpacity: 0.4 ,
fillColor: '#5dbac0',
fillOpacity: 0.4
});
let icon = new BMap.Icon(mapCenterPoint, new BMap.Size(20, 20));
let marker = new BMap.Marker(convertMapPoint(citys.centerPoint),{
icon
})
let msg = `<div id="fullViewMapInfo" class="full-view-map-info">
<h1>${citys.city}</h1>
<p>${citys.score}</p>
</div>`
let mapInfoBox = new BMapLib.InfoBox(map,msg,{
offset: new BMap.Size(10, 30),
boxStyle:{
},
closeIconUrl: closeImg,
enableAutoPan: false,
})
marker.name = ply.name = citys.city;
if(ply.name == that.nowCityName ){
ply.setStrokeColor('#fff');
ply.setStrokeOpacity(1);
ply.setStrokeWeight(2);
}
if(map && map.addOverlay){
let flag = true;
that.markerArr.forEach(item => {
if(ply.name == item.name ) {
flag = false;
}
})
if(flag) {
that.markerArr.push(marker);
map.addOverlay(marker);
}
map.addOverlay(ply);
}
let cityObj = {};
cityObj.city = citys.city;
cityObj.ply = ply;
let isArr = [];
isArr.push(cityObj);
let isArr2 = that.plyArr.concat(isArr);
that.plyArr = isArr2;
ply.addEventListener('click',function(e) {
mapInfoBox.open(e.point);
});
marker.addEventListener('click', function(e){
that.highLight(e.target.name,true);
})
}
}
}
}
map.clearOverlays();
改变地图缩放功能
@action changeMapZoom = (opt) => {
if(opt == 'add'){
this.mapZoom += 1;
if(this.mapZoom > 19){
this.mapZoom = 19;
}
this.map.setZoom(this.mapZoom);
} else {
this.mapZoom -= 1;
if(this.mapZoom < 4 ){
this.mapZoom = 4;
}
this.map.setZoom(this.mapZoom);
}
}
根据鼠标滚轮改变地图缩放
mapWheel = (id, zoom) => {
document.querySelector(id).onmousewheel = (e) => {
if(e.wheelDelta > 0) {
zoom = map.getZoom() + 1;
if (zoom > 19) {
zoom = 19
}
}
if (e.wheelDelta < 0) {
zoom = map.getZoom() - 1
if zoom < 4) {
zoom = 4
}
}
}
}