Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coordinates of Leaflet.Draw rectangle

Tags:

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

like image 684
mortenstarck Avatar asked Mar 07 '13 07:03

mortenstarck


People also ask

How do you use a leaflet draw plugin?

To use the Leaflet. draw plugin, you need to add the js and css files to your html header, like so: <! DOCTYPE html> <html> <head> <meta charset="UTF-8"> <!

How do you find the center of a polygon in a leaflet?

call one point of the polygon, then offset the position of the popup. Use an id for each polygon, so each popup knows the box area (polygon) it can be opened in.

How do you know if a point is inside a polygon leaflet?

Click on the map or the markers and a pop-up will show indicating if the point you clicked on, or the marker, is in the polygon. Leaflet.


1 Answers

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().

like image 124
tbicr Avatar answered Oct 03 '22 04:10

tbicr