美文网首页
openlayers 带箭头的线串

openlayers 带箭头的线串

作者: 花森文宝 | 来源:发表于2022-02-10 10:03 被阅读0次

请注意:这个方法无法解决两条及两条以上线完全重叠时产生的bug。

let newFeature = new ol.Feature({
    geometry: new olGeom.LineString(coordinates), 
    name
});
let geometry = newFeature.getGeometry();

let styles = (_, resolutions)=>{
    let s = [
        new olStyle.Style({
            color,width
        })
    ];

    let radio = (50 * resolution) / geometry.getLength();
    let dradio = 1;
    for (let i = 0; i <= 1; i += radio){
        let arrowLocation = geometry.getCoordinateAt(i);
        geometry.forEachSegment(function (start, end){
             // 避免图标离两端过近
            if (i < radio / 2 || i > 1-radio / 2) return;
            if (start[0] === end[0] || start[1] === end[1]) return;

            let dx1 = end[0] - arrowLocation[0];
            let dy1 = end[1] - arrowLocation[1];
            let dx2 = arrowLocation[0] - start[0];
            let dy2 = arrowLocation[1] - start[1];
            if (dx1 !== dx2 && dy1 !== dy2){
                if (Math.abs(dradio * dx1 * dy2 - dradio * dx2 *dy1) <0.001){
                    // 图标的朝向
                    let y = end[1] - start[1];
                    let x = end[0] - start[0];
                    let rotation = Math.atan(y / x);
                    if ((y <= 0 && x >= 0) || (x >= 0 && y >= 0)){
                        // 第一二象限
                        rotation = -rotation;
                    }else if ((x<= 0 && y >= 0) || (x <= 0 && y <= 0)){
                        rotation = Math.PI - rotation;
                    }

                    // 添加图标
                    s.push(
                        new olStyle.Style({
                            geometry: new olGeom.Point(arrowLocation),
                            image: new olStyle.Icon({
                                scale: width / 64 // 路网宽度 ÷ 图片大小,图标和路网宽度一样大小,
                                src: svgUrl,
                                anchor: [0.7, 0.5],
                                rotationWithView: false,
                                rotation: totation
                            })
                        })
                    );
                }
            }
        });
    }

    // 添加起点、终点图标
    s.push(
        new olStyle.Style({
            geometry: new olGeom.Point(position1),
            image: new olStyle.Icon({
                src: startUrl,
                anchor: [0.5, 1]
             })
        }),
        new olStyle.Style({
            geometry: new olGeom.Point(position2),
            image: new olStyle.Icon({
                src: endUrl,
                anchor: [0.5, 1]
             })
        })
     )
}

newFeature.setStyle(styles);

相关文章

  • openlayers 带箭头的线串

    请注意:这个方法无法解决两条及两条以上线完全重叠时产生的bug。

  • Tikz:好看的箭头

    说明:用Tikz中画带箭头的线时,只用[->]画的箭头太难了!推荐使用:[-latex]

  • OC 绘制带箭头的线

    绘图需求需要画带箭头的线,一开始想着绘制箭头好像很简单,类似Window开发有对应的lineCap属性去设置线。O...

  • openlayers-方向标注

    openlayers-方向标注 需求 给LineString 标注方向 效果图 设计思路 设计一个方向箭头图标 计...

  • ES6

    let 和const 解构赋值 字符串模板 字符串扩展函数 对象的简化写法 箭头函数 箭头函数的this 三点运算...

  • UML类图的关系模型讲解

    关系分类 泛化(继承) 实现 关联 组合 聚合 依赖 符号表示图 泛化 ** 带空心箭头的实线线表示 ** 泛化 ...

  • 使用Markdown画UML

    使用sequence 简单样式 复杂样式 mermaid 类型描述->实线无箭头->虚线无箭头->>带箭头的实线-...

  • 带箭头的UIView

    在网上查资料的时候,看到带箭头的UIView的箭头都是直的,显得很没有平滑感,恰好当时正在写高德地图,高德地图中...

  • 类图描述

    类图 带箭头的虚线表示类和接口的连接,带箭头的实线表示类和类之间的连接。 时序图

  • 标注箭头变细的教程

    标注箭头其实是一个块,输入BE命令(块编辑)然后选择ArchTick(箭头的块名称),改变箭头多段线的宽度就可以了...

网友评论

      本文标题:openlayers 带箭头的线串

      本文链接:https://www.haomeiwen.com/subject/vnknkrtx.html