Search code examples
javascriptgoogle-mapsarearectangles

Google Maps: How can I not let the user create a rectangle which has an area more than X?


I have set up a google map on my website(localhost) by using javascript. Right now the user can create circles or rectangles by left or right clicking. The user can also make the rectangle bigger or smaller and the same applies in circles as well. I would like the application not to let the user create rectangles which have an area more than X. I managed to achieve this with circles since the area of a circle is defined by its radius and Math.PI. I know that for the rectangle the area is the length of the bottom multiplied by the length of the height. However, how can I find this information from latitudes and longitudes of the south west and north east corners?

Thanks in advance


Solution

  • Let P(X1, Y1) and Q(X2, Y2) be the north east and south west coordinates (I suppose that you have converted them to pixels first). The rectangle's area would be then A = (X2-X1)*(Y2-Y1).