Javascript question: How do I calculate terminal coordinates from a lat/lng map coordinate given the distance and bearing

414 Views Asked by At

I am writing a javascript program which requires calculation of a terminal lat/lng coordinate given:

  1. source lat/lng coordinates
  2. bearing
  3. direction

signature required:

const getTerminalCoords(startCoords, bearing, distance) = () => {
  // mapping here
}

I have found lots of solutions in other languages even some using third party libraries but nothing cleanly using pure Javascript Math functions.

Any help or advice much appreciated.

1

There are 1 best solutions below

0
Terry Lennox On

You could try the very useful geolib.computeDestinationPoint, this will give you a destination point, given a bearing and distance:

const startPoint = { latitude: 52.518611, longitude: 13.408056 };
const distanceMeters = 15000;
const bearing = 180;

const destination = geolib.computeDestinationPoint(
    startPoint,
    distanceMeters,
    bearing 
);

console.log("Start point:", startPoint);
console.log("Distance (m), bearing (°):", distanceMeters, ",", bearing);
console.log("Destination point:", destination);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.js" crossorigin="anonymous"></script>