0

I want to get the coordinates of a point x meters away from a given point.

I found this code which gives the distance between two coordinate pairs but I want to give one coordinate pair and the distance and return the coordinates of the point away by the given distance.

I think an angle will need to be given too otherwise the result will be a circle. I got the following from this question

function measure(lat1, lon1, lat2, lon2){  // generally used geo measurement function
var R = 6378.137; // Radius of earth in KM
var dLat = lat2 * Math.PI / 180 - lat1 * Math.PI / 180;
var dLon = lon2 * Math.PI / 180 - lon1 * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d * 1000; // meters
}

Can someone help me do this in JavaScript?

I found another one that uses distance and bearing to do the same but using Google maps API. I don't understand the math behind the conversion and I don't want to include the Google maps API either.

this code is on This other question

Community
  • 1
  • 1
PeeBee
  • 149
  • 1
  • 1
  • 13

1 Answers1

0

From the question you referenced, look at this link and the section entitled "Destination point given distance and bearing from start point"

The math behind this is the haversine formula, which comes from spherical trigonometry. It is fine for a lot of situations, but it does have its limitations. In general I usually try to tell people not to try to do their calculations in geolocation/GIS if at all possible, as the math can get nasty much quicker than many realize. I should note as well that floating point precision can matter here, sometimes a lot.

sovemp
  • 1,402
  • 1
  • 13
  • 31