简体   繁体   中英

How to calculate the lat/lng of a point a certain distance away from another using Nodejs or javascript

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

在此处输入图片说明

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.

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)

在此处输入图片说明

So basically you need a function which will take current lat, long ie - (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)) 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM