mapBox 绘制多边形无法设置 边框宽度 解决方法

目录

一、问题

二、解决方法

 三、总结


tips:如嫌繁琐,直接看有颜色的文字即可!

一、问题

1.使用mapBox在地图上绘制点、线、面。绘制多边形的时候发现 直接用 zh(一家提供地图引擎的公司),提供的绘制多边形的方法无法设置边框颜色和边框宽度。很是离谱,按道理这种简单css就能实现的东西,为什么他们不能实现呢?况且fengMap是可以实现的。

二、解决方法

1.mapbox-gl-draw:下载链接https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js

2.方法一绘制完多边形后,再在多边形外面绘制 一条闭合的线段。于是写了如下的代码:

1)代码如下:


//线覆盖物基础参数
export const LineCoverConfig = {
  id: 'line',
  type: 'line',
  source: 'lineSource',
  paint: {
    'line-color': '#28e990',
    'line-width': 9,

  }
}

//多边形覆盖物样式
export const PolygonStyle = {
  color: '#ff5c2e',
  opacity: 0.5,
  height: 1,

}
//2)外边线样式(无法优化)
export const PolygonLineStyle = {
  id: 'polygonLine',
  type: 'line',
  source: 'polygonLineSource',
  paint: {
    'line-color': '#ff5c2e',
    'line-width': 2

  }
}      
    //根据坐标点添加线覆盖物 config:线覆盖物配置项
    addMark_line(pointArr, config, dataInfo) {
        const that = Locpard.instance;
        if (that && that.map) {
            let linePoint = []
            let option = Object.assign({}, LineCoverConfig, config);
            console.log("addMark_line", option, option.source, config)
            if (Array.isArray(pointArr)) {
                pointArr.forEach((element) => {
                    linePoint.push([element.y, element.x])
                })
                option.floor = pointArr[0]?.floor;
            }
            // that.map.drawLine( dataInfo.id, linePoint, option);

            this.drawLine(linePoint, option, dataInfo)
            const lineMarker = {
                id: option.id,
                source: option.source,
                type: 'layerLine'
            }
            return lineMarker;
        }
    }
    // 根据点集绘制 线段
    drawLine(pointArr, option, dataInfo) {
        const that = Locpard.instance;
        if (that) {
            that.map.map.addSource(option.source, {
                type: 'geojson',
                data: {
                    type: 'Feature',
                    properties: {
                        data: dataInfo
                    },
                    geometry: {
                        type: 'LineString',
                        coordinates: pointArr,
                    }
                }


            })
            that.map.map.addLayer(option)
        }
    }
    // 添加多边形
    addMark_polygon(option, dataInfo) {
        const that = Locpard.instance;
        if (that && that.map) {
            let pointArr = option.points.map((element) => {
                return [element.y, element.x]
            })
            let config = Object.assign({}, PolygonStyle, { ...option, floor: option.level })
            console.log("points", pointArr)
            let markerId = dataInfo.id.toString()
            //使用 zh 提供的api
            that.map.drawPolygon(markerId, pointArr, config)
            //手动 添加外边框
            that.addMark_line(option.points, { ...PolygonLineStyle, id: markerId, source: markerId }, dataInfo)
            return {
                id: markerId,
                source: markerId,
                type: "polygon"
            }
        }
    }

2)效果如下:

 3.方法二:总感觉方法一有问题,一次性渲染难道不是更好吗?为什么要去渲染两个图层,于是去mapBox找到绘制多边形的api,自己封装了一个绘制多边形的方法

1)代码如下:

//多边形覆盖物样式
export const PolygonStyle_2={
  id:'polygon',
  type:'fill',
  source:'polygonSource',
  layout:{},
  paint:{
    'fill-color':'#ff5c2e',
    'fill-opacity':0.5,
    'fill-outline-color':'#ff5c2e',
  }
}

    // can't set border-width
    addMark_polygon(option, dataInfo) {
        const that = Locpard.instance;
        if (that && that.map) {
            let pointArr = option.points.map((element) => {
                return [element.y, element.x]
            })
            option=Object.assign({},PolygonStyle_2,option)
            //that.map是 zh 封装的地图对象; that.map.map是 mapBox封装的地图对象
            that.map.map.addSource(option.source,{
                type:'geojson',
                data:{
                    type:'Feature',
                    geometry:{
                        type:'Polygon',
                        coordinates:[pointArr]
                    }
                }
            })
            that.map.map.addLayer(option)
            return {
                id:dataInfo.id.toString(),
                soureId:option.source,
                type: "polygon"
            }
        }
    }

2)效果如下: 虽然可以用fill-outline-color加边框了,仔细看一下可以看到边框的,但是不能设置边框的宽度。

尝试了 fill-outline-width,报错了,没有这个属性。去搜了一下gitHub上说:没有提供设置边框宽度的属性,因为WebGL不支持(会影响渲染速度)

 三、总结

1.所以如果你的需求是 设置边框颜色和边框宽度的话,就只能是 绘制两个图层:一个填充的多边形,外面再加一个 线覆盖图层(即采用方法一:其中的 that.map.drawPolygon(markerId, pointArr, config)==第三方厂家提供的方法 可以使用 方法二中的方法代替)

2.如果仅仅是添加边框颜色,则方法一和方法二都可以

3.不明白 fengMap难道不是用WebGL实现的?为什么它可以直接设置边框宽度呢?如有大佬知道,欢迎评论。

/*

希望对你有帮助!

如有错误,欢迎指正,非常感谢!

*/ 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值