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 = [];
      }
   });
};

 

posted @ 2019-02-20 16:43  卓扬  阅读(3286)  评论(1编辑  收藏  举报