• 官方文档 https://lbs.amap.com/api/jsapi-v2/guide/overlays/overlaygroup
  • 需求:根据屏幕范围显示当前路段车辆拥堵状态,鼠标拖动滚轮缩放更新数据,点击图例显示隐藏道路拥堵状态,根据道路拥堵状态为0-4级

    覆盖物群组 OverlayGroup

    1.gif ```javascript // 初始化地图 initMap() { this.map = new AMap.Map(‘map’, {}); this.map.on(‘complete’, () => { / 道路拥堵状态 / this.trafficJamState(); }) // 缩放级别begin============= AMap.event.addListener(this.map, ‘zoomend’, () => {
    if(this.showRoadlineOverlayGroup){
    1. this.initCreatRoadLink();
    } }); //地图平移结束begin============= AMap.event.addListener(this.map, ‘moveend’, () => { if(this.showRoadlineOverlayGroup){
    1. 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(); } }

  1. - 道路拥堵状态数据画线
  2. ```javascript
  3. export function setRoadLineArr(map, roadlineOverlayGroup, lineArr) {
  4. let unblockedPolyline;
  5. let tmpMap = map
  6. if(lineArr.color == '0'){
  7. for (let i = 0; i < lineArr.lines.length; i++) {
  8. // 绘制轨迹
  9. unblockedPolyline = new AMap.Polyline({
  10. map: tmpMap,
  11. path: lineArr.lines[i],
  12. showDir: true,
  13. strokeColor: "#34843D", //线颜色
  14. strokeWeight: 6, //线宽
  15. });
  16. // 创建覆盖物群组,传入覆盖物组成的数组
  17. roadlineOverlayGroup.addOverlay(unblockedPolyline);
  18. }
  19. }
  20. if(lineArr.color == '1'){
  21. // 绘制轨迹
  22. for (let i = 0; i < lineArr.lines.length; i++) {
  23. unblockedPolyline = new AMap.Polyline({
  24. map: tmpMap,
  25. path: lineArr.lines[i],
  26. showDir: true,
  27. strokeColor: "#8ACF26", //线颜色
  28. strokeWeight: 6, //线宽
  29. });
  30. // 创建覆盖物群组,传入覆盖物组成的数组
  31. roadlineOverlayGroup.addOverlay(unblockedPolyline);
  32. }
  33. }
  34. if(lineArr.color == '2'){
  35. // 绘制轨迹
  36. for (let i = 0; i < lineArr.lines.length; i++) {
  37. unblockedPolyline = new AMap.Polyline({
  38. map: tmpMap,
  39. path: lineArr.lines[i],
  40. showDir: true,
  41. strokeColor: "#ECEE1F", //线颜色
  42. strokeWeight: 6, //线宽
  43. });
  44. // 创建覆盖物群组,传入覆盖物组成的数组
  45. roadlineOverlayGroup.addOverlay(unblockedPolyline);
  46. }
  47. }
  48. if(lineArr.color == '3'){
  49. // 绘制轨迹
  50. for (let i = 0; i < lineArr.lines.length; i++) {
  51. unblockedPolyline = new AMap.Polyline({
  52. map: tmpMap,
  53. path: lineArr.lines[i],
  54. showDir: true,
  55. strokeColor: "#F38321", //线颜色
  56. strokeWeight: 6, //线宽
  57. });
  58. // 创建覆盖物群组,传入覆盖物组成的数组
  59. roadlineOverlayGroup.addOverlay(unblockedPolyline);
  60. }
  61. }
  62. if(lineArr.color == '4'){
  63. // 绘制轨迹
  64. for (let i = 0; i < lineArr.lines.length; i++) {
  65. unblockedPolyline = new AMap.Polyline({
  66. map: tmpMap,
  67. path: lineArr.lines[i],
  68. showDir: true,
  69. strokeColor: "#ED212C", //线颜色
  70. strokeWeight: 6, //线宽
  71. });
  72. // 创建覆盖物群组,传入覆盖物组成的数组
  73. roadlineOverlayGroup.addOverlay(unblockedPolyline);
  74. }
  75. }
  76. }