I found the following answer on Stackoverflow.
From what I understand, this is not the proper way to do it. The math is linear, however, the coordinates are mapped to a spherical surface. So what is the proper way to do it?
I have a function that calculates a midpoint, how can I alter it to accept a percentage as a parameter. In other words, find a midpoint that is between point 1 and 2 and is a percentage away from point 1...
middle_point(lat1, long1, lat2, long2) {
// Longitude difference.
var d_long = (long2 - long1) * Math.PI / 180;
// Convert to radians.
lat1 = lat1 * Math.PI / 180;
lat2 = lat2 * Math.PI / 180;
long1 = long1 * Math.PI / 180;
var b_x = Math.cos(lat2) * Math.cos(d_long);
var b_y = Math.cos(lat2) * Math.sin(d_long);
var lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + b_x) * (Math.cos(lat1) + b_x) + b_y * b_y));
var long3 = long1 + Math.atan2(b_y, Math.cos(lat1) + b_x);
// Return result.
return [long3 * 180/Math.PI, lat3 * 180/Math.PI];
}
The answer is given here: http://www.movable-type.co.uk/scripts/latlong.html and here: http://www.edwilliams.org/avform.htm#Intermediate
Here is the function you need, with "perc" between 0 and 100:
intermediate_point(lat1, long1, lat2, long2, perc) {
// Normalize percentage
perc = perc / 100;
// Convert to radians.
lat1 = lat1 * Math.PI / 180;
lat2 = lat2 * Math.PI / 180;
long1 = long1 * Math.PI / 180;
long2 = long2 * Math.PI / 180;
// get angular distance between points
var d_lat = lat2 - lat1;
var d_lon = long2 - long1;
var a = Math.sin(d_lat/2) * Math.sin(d_lat/2) + Math.cos(lat1) *
Math.cos(lat2) * Math.sin(d_lon/2) * Math.sin(d_lat/2);
var d = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var A = Math.sin((1-perc)*d) / Math.sin(d);
var B = Math.sin(perc*d) / Math.sin(d);
var x = A * Math.cos(lat1) * Math.cos(long1) + B * cos(lat2) * cos(long2);
var y = A * Math.cos(lat1) * Math.sin(long1) + B * cos(lat2) * sin(long2);
var z = A * Math.sin(lat1) + B * Math.sin(lat2);
var lat3 = Math.atan2(z, Math.sqrt(x*x + y*y));
var long3 = Math.atan2(y, x);
// Return result, normalising longitude to -180°...+180°
return [long3 * 180/Math.PI, (lat3 * 180/Math.PI + 540)%360-180];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With