How to perform GPS coordinates triangulation in JavaScript?

Viewed 503

I have two GPS coordinates and a distance from each point. I would like to automate the calculation of the two possible GPS coordinates that will match these constraints.

Any ideas on how to start?

2 Answers

You might use something like a reverse haversine function. We had some proposal on the python haversine package but nothing in production. https://github.com/mapado/haversine/pull/19

As Brad suggested, math might help you here. Once you get a formula representing your two circles, you "only" need to find the algorithm that gives you the intersection points (zero, two or infinite)

You can use the Turf.js library to perform advanced geospatial analysis in JavaScript. Triangulation is one the many possibilities.

Here is a code example of a triangulation to find the points that match two constraints of distance.

const center1 = [1.56, 44.58];
const distance1 = 127;
const circle1 = turf.circle(center1, distance1, {
  steps: 1000,
  units: "kilometers"
});

const center2 = [1.89, 44.97];
const distance2 = 89;
const circle2 = turf.circle(center2, distance2, {
  steps: 1000,
  units: "kilometers"
});

const intersections = turf.lineIntersect(circle1, circle2);
console.log(intersections);
// Outputs: [1.49712, 45.72128] and [3.01305, 45.07238]

Source: https://tzi.fr/js/triangulation/

Related