- 官方文档 https://lbs.amap.com/api/jsapi-v2/guide/overlays/overlaygroup
- 需求:根据屏幕范围显示当前路段车辆拥堵状态,鼠标拖动滚轮缩放更新数据,点击图例显示隐藏道路拥堵状态,根据道路拥堵状态为0-4级
覆盖物群组 OverlayGroup
```javascript // 初始化地图 initMap() { this.map = new AMap.Map(‘map’, {}); this.map.on(‘complete’, () => { / 道路拥堵状态 / this.trafficJamState(); }) // 缩放级别begin============= AMap.event.addListener(this.map, ‘zoomend’, () => {
if(this.showRoadlineOverlayGroup){
} }); //地图平移结束begin============= AMap.event.addListener(this.map, ‘moveend’, () => { if(this.showRoadlineOverlayGroup){this.initCreatRoadLink();
} });this.initCreatRoadLink();
}, / 初始化道路拥堵状态 / trafficJamState(){ this.creatRoadlayGroup(); this.creatRoadLink(); }, / 创建拥堵图层 / creatRoadlayGroup(){ this.roadlineOverlayGroup = createOverlayGroup(); this.map.add(this.roadlineOverlayGroup); this.oldRoadlineOverlayGroup = createOverlayGroup(); }, / 创建拥堵画线 / creatRoadLink(){ let arrayBounds = this.map.getBounds();
let leftTop = arrayBounds.path[0]; let leftTopGcj02 = gcj02towgs84(leftTop[0], leftTop[1]); let rightTop = arrayBounds.path[1]; let rightTopGcj02 = gcj02towgs84(rightTop[0], rightTop[1]); let rightDown = arrayBounds.path[2]; let rightDownGcj02 = gcj02towgs84(rightDown[0], rightDown[1]); let leftDown = arrayBounds.path[3]; let leftDownGcj02 = gcj02towgs84(leftDown[0], leftDown[1]);
getTrafficJamState({ regionId:this.regionId, leftTop:{lat:leftTopGcj02[1],lon:leftTopGcj02[0]}, leftDown:{lat:leftDownGcj02[1],lon:leftDownGcj02[0]}, rightTop:{lat:rightTopGcj02[1],lon:rightTopGcj02[0]}, rightDown:{lat:rightDownGcj02[1],lon:rightDownGcj02[0]}, }).then(res => { let data = res.data; if(data.length>0){ / 清空数据图层 / this.oldRoadlineOverlayGroup.setMap(null); data.forEach(item => { / 数据图层重新赋值 / setRoadLineArr(this.map, this.oldRoadlineOverlayGroup, item); }); setTimeout( ()=>{ / 数据图层赋值给数据展示图层 避免单图层操作鼠标滚轮拖动展示图层清空数据造成闪的问题 / this.roadlineOverlayGroup = this.oldRoadlineOverlayGroup; this.currenZoom = this.map.getZoom(); / 缩放级别大于15 并且 图例状态是否显示 / if(this.currenZoom > 15 && this.showRoadlineOverlayGroup){ this.roadlineOverlayGroup.show(); }else{ this.roadlineOverlayGroup.hide(); } }) } }) }, / 地图拖拽平移超过15级不请求接口 / initCreatRoadLink(){ this.currenZoom = this.map.getZoom(); if (this.currenZoom > 15) { this.creatRoadLink(); } }
- 道路拥堵状态数据画线
```javascript
export function setRoadLineArr(map, roadlineOverlayGroup, lineArr) {
let unblockedPolyline;
let tmpMap = map
if(lineArr.color == '0'){
for (let i = 0; i < lineArr.lines.length; i++) {
// 绘制轨迹
unblockedPolyline = new AMap.Polyline({
map: tmpMap,
path: lineArr.lines[i],
showDir: true,
strokeColor: "#34843D", //线颜色
strokeWeight: 6, //线宽
});
// 创建覆盖物群组,传入覆盖物组成的数组
roadlineOverlayGroup.addOverlay(unblockedPolyline);
}
}
if(lineArr.color == '1'){
// 绘制轨迹
for (let i = 0; i < lineArr.lines.length; i++) {
unblockedPolyline = new AMap.Polyline({
map: tmpMap,
path: lineArr.lines[i],
showDir: true,
strokeColor: "#8ACF26", //线颜色
strokeWeight: 6, //线宽
});
// 创建覆盖物群组,传入覆盖物组成的数组
roadlineOverlayGroup.addOverlay(unblockedPolyline);
}
}
if(lineArr.color == '2'){
// 绘制轨迹
for (let i = 0; i < lineArr.lines.length; i++) {
unblockedPolyline = new AMap.Polyline({
map: tmpMap,
path: lineArr.lines[i],
showDir: true,
strokeColor: "#ECEE1F", //线颜色
strokeWeight: 6, //线宽
});
// 创建覆盖物群组,传入覆盖物组成的数组
roadlineOverlayGroup.addOverlay(unblockedPolyline);
}
}
if(lineArr.color == '3'){
// 绘制轨迹
for (let i = 0; i < lineArr.lines.length; i++) {
unblockedPolyline = new AMap.Polyline({
map: tmpMap,
path: lineArr.lines[i],
showDir: true,
strokeColor: "#F38321", //线颜色
strokeWeight: 6, //线宽
});
// 创建覆盖物群组,传入覆盖物组成的数组
roadlineOverlayGroup.addOverlay(unblockedPolyline);
}
}
if(lineArr.color == '4'){
// 绘制轨迹
for (let i = 0; i < lineArr.lines.length; i++) {
unblockedPolyline = new AMap.Polyline({
map: tmpMap,
path: lineArr.lines[i],
showDir: true,
strokeColor: "#ED212C", //线颜色
strokeWeight: 6, //线宽
});
// 创建覆盖物群组,传入覆盖物组成的数组
roadlineOverlayGroup.addOverlay(unblockedPolyline);
}
}
}