代码
var map = new BMap.Map('map');
var center = new BMap.Point(116.668, 39.689);
map.centerAndZoom(center, 10);
map.enableScrollWheelZoom(true);

var isDashed = false;
var weight = 6;
var isTranslucent = false;
var colors = ['#2a6cf6', '#e7515a', '#1aa179', '#8b5cf6', '#f59e0b'];
var colorIdx = 0;

// 创建折线可视化图层,适合批量绘制 GeoJSON 线数据
var polylineLayer = new BMap.PolylineLayer({
  strokeColor: colors[colorIdx],
  strokeWeight: weight,
  strokeStyle: 'solid',
  strokeLineCap: 'round',
  strokeLineJoin: 'round',
  strokeOpacity: 1,
  referCenter: center,
});

map.addLayer(polylineLayer);

// PolylineLayer 支持 LineString 和 MultiLineString 类型的 GeoJSON 数据
fetch('https://mapopen-pub-jsapigl.bj.bcebos.com/svgmodel/lineLayerData.json')
  .then(function (res) {
    return res.json();
  })
  .then(function (geojson) {
    polylineLayer.setData(geojson);
  })
  .catch(function (err) {
    console.error('线数据加载失败:', err);
  });

// 切换实线 / 虚线
function toggleDash() {
  isDashed = !isDashed;
  polylineLayer.setOptions({
    strokeStyle: isDashed ? 'dashed' : 'solid',
    dashArray: [20, 14], // [实线 px, 空白 px]
  });
  var btn = document.getElementById('btn-dash');
  btn.textContent = isDashed ? '切换实线' : '切换虚线';
  btn.classList.toggle('is-active', isDashed);
}

// 修改线宽(6→8→10→12 循环)
function thicken() {
  weight = weight >= 12 ? 6 : weight + 2;
  polylineLayer.setOptions({
    strokeWeight: weight,
  });
}

// 修改线条颜色
function changeColor() {
  colorIdx = (colorIdx + 1) % colors.length;
  polylineLayer.setOptions({
    strokeColor: colors[colorIdx],
  });
}

// 切换线条透明度
function toggleOpacity() {
  isTranslucent = !isTranslucent;
  polylineLayer.setOptions({
    strokeOpacity: isTranslucent ? 0.35 : 1,
  });
  var btn = document.getElementById('btn-opacity');
  btn.textContent = isTranslucent ? '恢复透明度' : '降低透明度';
  btn.classList.toggle('is-active', isTranslucent);
}

通过 new BMap.PolylineLayer 创建折线图层,使用 setData 设置 GeoJSON 线数据,并通过 setOptions 动态调整 strokeStyle、strokeWeight、strokeColor、strokeOpacity 等样式。

预览