Coordinates of Leaflet.Draw rectangle

mortenstarck picture mortenstarck · Mar 7, 2013 · Viewed 19.5k times · Source

Is it possible to get the coordinates of the rectangle on mouseClick, so I have all the corners of the rectangle?

Answer

tbicr picture tbicr · Mar 7, 2013

See event object (http://leafletjs.com/reference.html#event-objects):

var map = L.map('map').setView([53.902257, 27.561640], 13);

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);

var bounds = [[53.912257, 27.581640], [53.902257, 27.561640]];

var rect = L.rectangle(bounds, {color: 'blue', weight: 1}).on('click', function (e) {
    // There event is event object
    // there e.type === 'click'
    // there e.lanlng === L.LatLng on map
    // there e.target.getLatLngs() - your rectangle coordinates
    // but e.target !== rect
    console.info(e);
}).addTo(map);

Use e.target.getLatLngs().