Calculate point between two coordinates based on a

2019-02-09 14:10发布

问题:

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.

回答1:

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%).



回答2:

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.