Leaflet绘制多边形


<div id="map"></div>
 
var map = L.map('map').setView([51.505, -0.09], 13);
 
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{ attribution: '© <a href="#">OpenStreetMap</a>' }).addTo(map);
 
let points = [];
 
const polygon = new L.Polygon(points);
map.addLayer(polygon);
 
map.on('mousedown', e => {
    points.push([e.latlng.lat, e.latlng.lng]);
 
    map.on('mousemove', event => {
        polygon.setLatLngs([...points,[event.latlng.lat, event.latlng.lng]])
    });
});
 
map.on('dblclick', () => {
   if (points.length) {
        map.off('mousemove');
        points = [];
      }
   });
};

 


免责声明!

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



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