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