Warm tip: This article is reproduced from serverfault.com, please click

How to draw rectangle marker in leaflet given only 1 [lat,lon] pair and sizes a and b in meters

发布于 2020-11-30 18:09:42

I need to draw a rectangle around a point in leaflet map. The point is a pair [lat,lon] and I have the sizes of the sides of the rectangle in meters. How can I easily do that?

Questioner
Eberson Santos
Viewed
0
PeaceLeka 2020-12-01 16:02:46

Assuming the point will be in the center of the rectangle, you can try use the Leaflet.GeometryUtil's destination to find bounds add rectangle based on the bounds. Not tested.

var latlng = L.latLng(lat, lon);

//Assuming rectangle has side A and side B
hypotenuse = Math.sqrt((0.5*lengthofA)**2 + (0.5*lengthofB)**2) 
swAngle = 90 + Math.atan(lengthofA/lengthofB)*180/Math.PI
neAngle = 0 - Math.atan(lengthofA/lengthofB)*180/Math.PI

var southWest = destination(latlng, swAngle, hypotenuse) //returns point L.latlng
var northEast = destination(latlng, neAngle, hypotenuse)
var bounds = L.latLngBounds(southWest, northEast);
var rectangle = L.rectangle(bounds).addTo(map);