Leaflet中使用leaflet.polylineDecorator插件绘制箭头线及虚线矩形


场景

Leaflet快速入门与加载OSM显示地图:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/122290880

在上面的基础上怎样绘制箭头、虚线矩形。

 

 

Leaflet.PolylineDecorator插件地址:

https://github.com/bbecquet/Leaflet.PolylineDecorator

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、下载插件并引入依赖

<script type="text/javascript" src="./js/leaflet.polylineDecorator.js"></script>

2、绘制箭头

        //绘制图层
        var drawnItems = new L.FeatureGroup();
                    //添加绘制图层
                    map.addLayer(drawnItems);

        //1.绘制线
        var arrow = L.polyline([[36.09, 120.35], [36.10, 120.38]], {
            //颜色
            color: 'red'
        }).addTo(drawnItems);
        //添加箭头
        var arrowHead = L.polylineDecorator(arrow, {
            //添加模式
            patterns: [{
                //模式符号的偏移位置
                offset: '100%',
                //模式符号的重复间隔
                repeat: 0,
                //符号实例
                symbol: L.Symbol.arrowHead({
                    //符号大小
                    pixelSize: 15,
                    //符号样式
                    pathOptions: {
                        //是否显示边线
                        stroke: true
                    }
                })
            }]
        }).addTo(drawnItems);

3、绘制虚线矩形

        //2.绘制虚线矩形
        var pathPattern = L.polylineDecorator([[36.11, 120.30], [36.11, 120.33], [36.14, 120.33], [36.14, 120.30], [36.11, 120.30]], {
            //添加模式
            patterns: [{
                //模式符号的偏移位置
                offset: 12,
                //模式符号的重复间隔
                repeat: 25,
                //符号实例
                symbol: L.Symbol.dash({
                    //符号大小
                    pixelSize: 10,
                    //符号样式
                    pathOptions: {
                        //颜色
                        color: '#f00',
                        //线宽
                        weight: 2
                    }
                })
            },
                {
                    //模式符号的偏移位置
                    offset: 0,
                    //模式符号的重复间隔
                    repeat: 25,
                    //符号实例
                    symbol: L.Symbol.dash({
                        //符号大小
                        pixelSize: 0
                    })
                }
            ]
        }).addTo(drawnItems);

4、绘制飞行航线图

        //3.绘制图案
        var pathPattern = L.polylineDecorator([[36.15, 120.38], [36.14, 120.39], [36.13, 120.42], [36.11, 120.44], [36.09, 120.49]], {
            //添加模式
            patterns: [
                {
                    //模式符号的偏移位置
                    offset: 0,
                    //模式符号的重复间隔
                    repeat: 10,
                    //符号实例
                    symbol: L.Symbol.dash({
                        //符号大小
                        pixelSize: 5,
                        //符号样式
                        pathOptions: {
                            //颜色
                            color: 'blue',
                            //线宽
                            weight: 1,
                            //透明度
                            opacity: 1
                        }
                    })
                },
                {
                    //模式符号的偏移位置
                    offset: '0%',
                    //模式符号的重复间隔
                    repeat: '20%',
                    //符号实例
                    symbol: L.Symbol.marker({
                        //是否允许旋转
                        rotate: true,
                        //标记显示样式
                        markerOptions: {
                            //图标
                            icon: L.icon({
                                //图标地址
                                iconUrl: './images/icon_plane.png',
                                //图标位置
                                iconAnchor: [16, 16]
                            })
                        }
                    })
                }
            ]
        }).addTo(drawnItems);

5、完整示例代码

<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>leaflet绘制箭头和虚线矩形</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
    <style>
        html,
        body,
        #map {
            padding: 0;
            margin: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div id="map"></div>
    <script type="text/javascript" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
    <script type="text/javascript" src="./js/leaflet.polylineDecorator.js"></script>
    <script type="text/javascript">
        var map = L.map('map').setView([36.09, 120.35], 13);
        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: ''
        }).addTo(map);

        //绘制图层
        var drawnItems = new L.FeatureGroup();
                    //添加绘制图层
                    map.addLayer(drawnItems);

        //1.绘制线
        var arrow = L.polyline([[36.09, 120.35], [36.10, 120.38]], {
            //颜色
            color: 'red'
        }).addTo(drawnItems);
        //添加箭头
        var arrowHead = L.polylineDecorator(arrow, {
            //添加模式
            patterns: [{
                //模式符号的偏移位置
                offset: '100%',
                //模式符号的重复间隔
                repeat: 0,
                //符号实例
                symbol: L.Symbol.arrowHead({
                    //符号大小
                    pixelSize: 15,
                    //符号样式
                    pathOptions: {
                        //是否显示边线
                        stroke: true
                    }
                })
            }]
        }).addTo(drawnItems);

        //2.绘制虚线矩形
        var pathPattern = L.polylineDecorator([[36.11, 120.30], [36.11, 120.33], [36.14, 120.33], [36.14, 120.30], [36.11, 120.30]], {
            //添加模式
            patterns: [{
                //模式符号的偏移位置
                offset: 12,
                //模式符号的重复间隔
                repeat: 25,
                //符号实例
                symbol: L.Symbol.dash({
                    //符号大小
                    pixelSize: 10,
                    //符号样式
                    pathOptions: {
                        //颜色
                        color: '#f00',
                        //线宽
                        weight: 2
                    }
                })
            },
                {
                    //模式符号的偏移位置
                    offset: 0,
                    //模式符号的重复间隔
                    repeat: 25,
                    //符号实例
                    symbol: L.Symbol.dash({
                        //符号大小
                        pixelSize: 0
                    })
                }
            ]
        }).addTo(drawnItems);

        //3.绘制图案
        var pathPattern = L.polylineDecorator([[36.15, 120.38], [36.14, 120.39], [36.13, 120.42], [36.11, 120.44], [36.09, 120.49]], {
            //添加模式
            patterns: [
                {
                    //模式符号的偏移位置
                    offset: 0,
                    //模式符号的重复间隔
                    repeat: 10,
                    //符号实例
                    symbol: L.Symbol.dash({
                        //符号大小
                        pixelSize: 5,
                        //符号样式
                        pathOptions: {
                            //颜色
                            color: 'blue',
                            //线宽
                            weight: 1,
                            //透明度
                            opacity: 1
                        }
                    })
                },
                {
                    //模式符号的偏移位置
                    offset: '0%',
                    //模式符号的重复间隔
                    repeat: '20%',
                    //符号实例
                    symbol: L.Symbol.marker({
                        //是否允许旋转
                        rotate: true,
                        //标记显示样式
                        markerOptions: {
                            //图标
                            icon: L.icon({
                                //图标地址
                                iconUrl: './images/icon_plane.png',
                                //图标位置
                                iconAnchor: [16, 16]
                            })
                        }
                    })
                }
            ]
        }).addTo(drawnItems);
    </script>
</body>

</html>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM