Leaflet中使用leaflet.polylineDecorator插件绘制箭头线及虚线矩形
生活随笔
收集整理的這篇文章主要介紹了
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>?總結
以上是生活随笔為你收集整理的Leaflet中使用leaflet.polylineDecorator插件绘制箭头线及虚线矩形的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leaflet中绘制同心圆、多个中心对称
- 下一篇: Leaflet中使用Leaflet.An