場景
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>
