Skip to content
Advertisement

leaflet square given centre and square width

On Leaflet I can create a new circle easily given the centre and the radius:

JavaScript

The circle above is created and drawn without problems, so it is all.

However, if I wanted now to create and draw a rectangle that which bounds the circle, it does not work. Here is what I did:

JavaScript

The size of the rectangle that I obtain has nothing to do with 500 metres. Also, it looks like the size of the rectangle depends on the zoom level the map is. None of these problems arose for the circle.

I suspect the way I transform the latitude/longitude to point and viceversa is wrong.

Advertisement

Answer

Just use the getBounds method that L.Circle inherits from L.Path:

Returns the LatLngBounds of the path.

http://leafletjs.com/reference.html#path-getbounds

JavaScript

Working example on Plunker: http://plnkr.co/edit/n55xLOIohNMY6sVA3GLT?p=preview

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement