How to get latlng of a rectangle created with leaflet.draw addon

1,443 views
Skip to first unread message

da...@southtrader.co.za

unread,
Mar 25, 2013, 10:40:10 AM3/25/13
to leafl...@googlegroups.com
Hi

I am using the leaflet draw addon. I am trying to get the latlng of the corners of rectangles drawn onto the screen by the user.
Any help with this will be appreciated

thanks

Shane McCarthy

unread,
Mar 25, 2013, 4:54:57 PM3/25/13
to leafl...@googlegroups.com
I'm not sure if this would be the best way, but as the user draws their rectangle I would capture the latlng at the point when they click the map and store it in an array for use.

da...@southtrader.co.za

unread,
Mar 25, 2013, 5:11:34 PM3/25/13
to leafl...@googlegroups.com
Surely there is a way to access the latlng of the rectangles that were drawn???

Shane McCarthy

unread,
Mar 26, 2013, 5:27:12 AM3/26/13
to leafl...@googlegroups.com
I'm new to leaflet so I'm probably not the best person to be answering...

Regardless,
Have a read of this answer
http://stackoverflow.com/questions/15244798/find-associated-polylines-for-selected-marker-circlemarker-point-in-leaflet

The person states using the getPolyline function
https://github.com/Leaflet/Leaflet/blob/master/src/layer/vector/Polyline.js#L34

DavidG

unread,
Mar 26, 2013, 6:10:55 AM3/26/13
to leafl...@googlegroups.com
Thanks Shane, that did help. I have got it working. Below is a snippet with the alert showing the resul. In the finished code the alert will be replaced by code which will store it in a DB

        map.on('draw:created', function (e) {
            var type = e.layerType,
                layer = e.layer;
                alert(layer.getLatLngs());
                drawnItems.addLayer(layer);
        });

Shane McCarthy

unread,
Mar 26, 2013, 8:08:10 AM3/26/13
to leafl...@googlegroups.com
Great!
Reply all
Reply to author
Forward
0 new messages