# Calculate point between two coordinates based on a percentage

I am looking for a function that returns a point (lat, long) between two points (where I also specify their lat, long) and that point is based on a distance percentage.

So, I specify Lat1, Lon1, Lat2, Lon2 and a % on the function and, it returns a point, for example, 20% distant from the first point to the second.

Assuming the coordinate is a decimal number. You can use this equation.

``````function midpoint(lat1, long1, lat2, long2, per) {
return [lat1 + (lat2 - lat1) * per, long1 + (long2 - long1) * per];
}
``````

Return a new desired coordinate of [lat, long], based on the percentage (such as per=0.2 for 20%).

Here is a reference that will help a lot (check the bottom)

http://www.movable-type.co.uk/scripts/latlong.html

Intermediate point

An intermediate point at any fraction along the great circle path between two points can also be calculated.

Formula:

``````a = sin((1?f)??) / sin ?
b = sin(f??) / sin ?
x = a ? cos ?1 ? cos ?1 + b ? cos ?2 ? cos ?2
y = a ? cos ?1 ? sin ?1 + b ? cos ?2 ? sin ?2
z = a ? sin ?1 + b ? sin ?2
?i = atan2(z, ?x² + y²)
?i = atan2(y, x)
``````

where f is fraction along great circle route (f=0 is point 1, f=1 is point 2), ? is the angular distance d/R between the two points.

Both answers might be useful for specific users, but I would like to point out some issues.

The solution by lguiel is correct for games or short distances, but does not work for geometric calculations on the earth as a globe.

The answer by spirographer is correct, but as it is very theoretical it might be too difficult to program.

I translated the second answer to a practical programming language, so that you can use it for your own projects.

``````// Original calculation from https://www.movable-type.co.uk/scripts/latlong.html
LatLng calculateIntermediatePoint(LatLng point1, LatLng point2, double perc) {

//const ?? = ?2 - ?1;
//const ?? = ?2 - ?1;
double deltaLat = lat2 - lat1;
double deltaLng = lng2 - lng1;

//const a = Math.sin(??/2) * Math.sin(??/2) + Math.cos(?1) * Math.cos(?2) * Math.sin(??/2) * Math.sin(??/2);
//const ? = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double calcA = sin(deltaLat / 2) * sin(deltaLat / 2) +
cos(lat1) * cos(lat2) * sin(deltaLng / 2) * sin(deltaLng / 2);
double calcB = 2 * atan2(sqrt(calcA), sqrt(1 - calcA));

//const A = Math.sin((1-fraction)*?) / Math.sin(?);
//const B = Math.sin(fraction*?) / Math.sin(?);
double A = sin((1 - perc) * calcB) / sin(calcB);
double B = sin(perc * calcB) / sin(calcB);

//const x = A * Math.cos(?1) * Math.cos(?1) + B * Math.cos(?2) * Math.cos(?2);
//const y = A * Math.cos(?1) * Math.sin(?1) + B * Math.cos(?2) * Math.sin(?2);
//const z = A * Math.sin(?1) + B * Math.sin(?2);
double x = A * cos(lat1) * cos(lng1) + B * cos(lat2) * cos(lng2);
double y = A * cos(lat1) * sin(lng1) + B * cos(lat2) * sin(lng2);
double z = A * sin(lat1) + B * sin(lat2);

//const ?3 = Math.atan2(z, Math.sqrt(x*x + y*y));
//const ?3 = Math.atan2(y, x);
double lat3 = atan2(z, sqrt(x * x + y * y));
double lng3 = atan2(y, x);

//const lat = ?3.toDegrees();
//const lon = ?3.toDegrees();