0

I need to find a square area using a latitude and longitude(x,y) as the following figure

enter image description here

I need to get all the other 3 corner latitude and longitude by adding 10kms to each side. I am using Node.js/javascript to implement this.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

2

Referring to the below geometry diagram, the only co-ordinate you need to calculate is - (x2, y2) and rest of the two co-ordinate you can calculate using current long, lat - (x1, y1) and computed - (x2, y2)

enter image description here

So basically you need a function which will take current lat, long i.e. - (x1, y1), a distance which is √2 * 10km in your example and bearing angle to point (x2, y2) which at 135 degrees.

let llFromDistance = function(latitude, longitude, distance, bearing) {
  // taken from: https://stackoverflow.com/a/46410871/13549 
  // distance in KM, bearing in degrees

  const R = 6378.1; // Radius of the Earth
  const brng = bearing * Math.PI / 180; // Convert bearing to radian
  let lat = latitude * Math.PI / 180; // Current coords to radians
  let lon = longitude * Math.PI / 180;

  // Do the math magic
  lat = Math.asin(Math.sin(lat) * Math.cos(distance / R) + Math.cos(lat) * Math.sin(distance / R) * Math.cos(brng));
  lon += Math.atan2(Math.sin(brng) * Math.sin(distance / R) * Math.cos(lat), Math.cos(distance / R) - Math.sin(lat) * Math.sin(lat));

  // Coords back to degrees and return
  return [(lat * 180 / Math.PI), (lon * 180 / Math.PI)];

}

console.log(llFromDistance(19.0659115, 72.8574557, Math.sqrt(2)*10, 135))
Suresh Prajapati
  • 3,991
  • 5
  • 26
  • 38
1

Here's a function I've used - not sure of it's usefulness when close to the poles though

const fn = (latitude, longitude, distanceInKm, bearingInDegrees) => {
    const R = 6378.1;
    const dr = Math.PI / 180;
    const bearing = bearingInDegrees * dr;
    let lat = latitude * dr;
    let lon = longitude * dr;

    lat = Math.asin(Math.sin(lat) * Math.cos(distanceInKm / R) + Math.cos(lat) * Math.sin(distanceInKm / R) * Math.cos(bearing));
    lon += Math.atan2(
        Math.sin(bearing) * Math.sin(distanceInKm / R) * Math.cos(lat), 
        Math.cos(distanceInKm / R) - Math.sin(lat) * Math.sin(lat)
    );
    lat /= dr;
    lon /= dr;
    return {lat, lon};
}

so, the points would be

fn(y, x, 10, 90), // top right
fn(y, x, 10 * Math.sqrt(2), 135), // bottom right (Pythagoras rules!)
fn(y, x, 10, 180) // bottom left
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87