两个坐标之间的中点地理 [英] Geographic Midpoint between two coordinates
问题描述
我一直用可移动型网站,以帮助我在一些会有地理座标calcuations,它已经非常有益的,但是,我有一个错误在我的两个坐标之间的中点计算。我的结果是接近预期,但不够紧密:
I have been using the Moveable-Type website to aid me in some Geocoordinate calcuations and it's been very useful, however, I have a bug in my calculation of the mid-point between two coordinates. My result is close to the expected, but not close enough:
posA = {47.64570362, -122.14073746}
posB = {47.64316917, -122.14032175}
预期的结果(从活字印刷计算器拍摄)= 47°38 '40N,122°08'26W = {47.644444,-122.140556}
我的结果: {49.6054801645915,-122.14052959995759}
expected result (taken from the movable type calculator) = 47°38′40″N, 122°08′26″W = {47.644444, -122.140556}
my result: {49.6054801645915, -122.14052959995759}
下面是我的代码:
private Geocoordinate MidPoint(Geocoordinate posA, Geocoordinate posB)
{
Geocoordinate midPoint = new Geocoordinate();
double dLon = DegreesToRadians(posB.Longitude - posA.Longitude);
double Bx = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Cos(dLon);
double By = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Sin(dLon);
midPoint.Latitude = RadiansToDegrees(Math.Atan2(Math.Sin(DegreesToRadians(posA.Latitude)) + Math.Sin(DegreesToRadians(posB.Latitude)),
Math.Sqrt((Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) * (Math.Cos(DegreesToRadians(posA.Latitude))) + Bx) + By * By));
midPoint.Longitude = posA.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(posA.Latitude)) + Bx));
return midPoint;
}
我有一对夫妇的私有方法来做度之间的转换和弧度和背部。
例如:
I've got a couple of private methods to do the conversion between Degrees and Radians and back. E.g.
private double DegreeToRadian(double angle)
{
return Math.PI * angle / 180.0;
}
我不明白为什么我的结果是关闭几度在纬度值。任何想法?
I can't work out why my results are off by a couple of degrees on the Lat value. Any ideas?
感谢
推荐答案
您摆放着一些括号错了。标志着我在代码的地方。
You placed some parentheses wrong. I marked the place in the code.
private Geocoordinate MidPoint(Geocoordinate posA, Geocoordinate posB)
{
Geocoordinate midPoint = new Geocoordinate();
double dLon = DegreesToRadians(posB.Longitude - posA.Longitude);
double Bx = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Cos(dLon);
double By = Math.Cos(DegreesToRadians(posB.Latitude)) * Math.Sin(dLon);
midPoint.Latitude = RadiansToDegrees(Math.Atan2(
Math.Sin(DegreesToRadians(posA.Latitude)) + Math.Sin(DegreesToRadians(posB.Latitude)),
Math.Sqrt(
(Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) *
(Math.Cos(DegreesToRadians(posA.Latitude)) + Bx) + By * By)));
// (Math.Cos(DegreesToRadians(posA.Latitude))) + Bx) + By * By)); // Your Code
midPoint.Longitude = posA.Longitude + RadiansToDegrees(Math.Atan2(By, Math.Cos(DegreesToRadians(posA.Latitude)) + Bx));
return midPoint;
}
这篇关于两个坐标之间的中点地理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!