该代码主要用于在地图上绘制多线层,并支持线段的交互和样式控制,适用于展示交通网络、物流配送等场景。
const BMapGL = window.BMapGL
var map = new BMapGL.Map('allmap', {
displayOptions: {
poiText: false,
building: false,
},
})
fetch(
'https://mapopen-pub-jsapigl.bj.bcebos.com/svgmodel/mutiLineLayerData.json',
).then((res) => res.json()).then((testLineData) => {
// 创建多线层对象
var lineLayer = new BMapGL.LineLayer({
zIndex: 6,
enablePicked: true,
pickWidth: 30,
pickHeight: 30,
opacity: 1,
selectedColor: 'blue', // 选中项颜色
style: {
strokeColorControl: (index, link) => {
return ['#a9f49e', '#3faf7c', 'pink', '#3faf7c'][link % 4]
},
sequence: true,
marginLength: 32,
borderColor: 'green',
borderMask: false,
borderWeight: 2,
strokeWeight: 6,
strokeLineJoin: 'miter',
strokeLineCap: 'round',
strokeTextureUrl:
'https://mapopen-pub-jsapigl.bj.bcebos.com/demo/img/img2.png',
strokeTextureWidth: 32,
strokeTextureHeight: 64,
},
})
lineLayer.addEventListener('click', function (e) {
if (e.value.dataIndex !== -1 && e.value.dataItem) {
console.log(e.value.dataItem)
} else {
console.log('未选中')
}
})
map.addNormalLayer(lineLayer)
lineLayer.setData(testLineData)
})
function addLineLayer() {
// 添加多线层到地图中
map.addNormalLayer(lineLayer)
}
function removeLineLayer() {
// 移除多线层
map.removeNormalLayer(lineLayer)
}
lineLayer.addEventListener('click', function (e) {
if (e.value.dataIndex !== -1 && e.value.dataItem) {
console.log(e.value.dataItem)
} else {
console.log('未选中')
}
})