5

I have the center point coordinates (lng, lat) of an area for which I would like to calculate the coordinates (lng, lat) of the four corners in a bounding box around this center point. the distance from the center to each corner is 10 meters.

Illustration:

enter image description here

How do I do this in Javascript?

Is there a library for such a thing or must I dust off the part of my brain that spent years ignoring Mrs Allard's trigonometry class?

Zigglzworth
  • 6,645
  • 9
  • 68
  • 107

3 Answers3

6

Based on the answer given by Bruno Pinto here I created and used this in the end:

function getBoundingBox(pLatitude, pLongitude, pDistanceInMeters) {


            var latRadian = pLatitude.toRad();

            var degLatKm = 110.574235;
            var degLongKm = 110.572833 * Math.cos(latRadian);
            var deltaLat = pDistanceInMeters / 1000.0 / degLatKm;
            var deltaLong = pDistanceInMeters / 1000.0 / degLongKm;


            var topLat = pLatitude + deltaLat;
            var bottomLat = pLatitude - deltaLat;
            var leftLng = pLongitude - deltaLong;
            var rightLng = pLongitude + deltaLong;

            var northWestCoords = topLat + ',' + leftLng;
            var northEastCoords = topLat + ',' + rightLng;
            var southWestCoords = bottomLat + ',' + leftLng;
            var southEastCoords = bottomLat + ',' + rightLng;

            var boundingBox = [northWestCoords, northEastCoords, southWestCoords, southEastCoords];


            return boundingBox;
}

if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
  return this * Math.PI / 180;
 }
}
Community
  • 1
  • 1
Zigglzworth
  • 6,645
  • 9
  • 68
  • 107
0

You can have a look at the following function :-

/*The getRectCoordinates takes the coordinates of the center point 
and the radial distance at which you want to find the coordinates*/
function getRectCoordinates (x,y,r) {
 //theta is the angle hard-coded to 45 degrees to think as the square 
 var theta = Math.PI/4,
 cos = Math.cos,
 sin = Math.sin;
 return {
  'topRight' : {
   'x' : x + r*cos(theta),
   'y' : y + r*sin(theta)
  },
  'bottomRight' : {
   'x' : x + r*cos(theta),
   'y' : y - r*sin(theta)
  },
  'topLeft' : {
   'x' : x - r*cos(theta),
   'y' : y + r*sin(theta)
  },
  'bottomLeft' : {
   'x' : x + r*cos(theta),
   'y' : y - r*sin(theta)
  }
 }
}

//An example for this (Check console for output): 
console.log(getRectCoordinates(2,3,10));

Let me know if your problem is solved brother.

Ayan
  • 2,300
  • 1
  • 13
  • 28
0

Let (x, y) be the coordinates of the center, as indicated in the image below:

enter image description here

Then you can actually calculate the coordinates in the following way:

/* Returns an array of arrays, each one corresponding to each point's
   coordinates (x, y) */
function getCoordinatesSquare (distance_to_center, x, y)
{
    var points = [];
    var dx = distance_to_center * Math.sin(Math.PI / 4);
    var dy = distance_to_center * Math.cos(Math.PI / 4);

    points[0] = [x - dx, y - dy]; // (x1, y1)
    points[1] = [x - dx, y + dy]; // (x2, y2)
    points[2] = [x + dx, y + dy]; // (x3, y3)
    points[3] = [x + dx, y - dy]; // (x4, y4)

    return points;
}

/* Set the initial conditions. */
var distance_to_center = 10;
var x = 4;
var y = 5;

/* Calculate. */
var coordinates = getCoordinatesSquare (distance_to_center, x, y);

/* Test the results. */
for (var i = 0; i < coordinates.length; i++){
    console.log ("(" + coordinates[i][0] + ", " + coordinates[i][1] + ") \n");
}

And in this case the output will be:

(-3.0710678118654746, -2.0710678118654755)
(-3.0710678118654746, 12.071067811865476) 
(11.071067811865476, 12.071067811865476) 
(11.071067811865476, -2.0710678118654755) 

And plotting the points:

enter image description here

Community
  • 1
  • 1
  • This didn't get me the results I needed.. possibly due to the distance not being converted – Zigglzworth Oct 20 '15 at 13:17
  • @Zigglzworth What results do you need? I understood you were asking how to calculate the coordinates of a square given its center position... And what is exactly the distance conversion you are talking about? – Daniel Muñoz Parsapoormoghadam Oct 20 '15 at 13:21
  • My question specifically applies to geo-coordinates and distances in meters. In applying your solution with a set of real coordinates I found that the resulting 4 coordinates were incorrect (far away from the center point). I assumed that is due to distance conversion issues – Zigglzworth Oct 20 '15 at 14:29