[英]Geographic Midpoint between two coordinates
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. 我一直在使用Moveable-Type网站来帮助我进行一些Geocoordinate计算并且它非常有用,但是,我在计算两个坐标之间的中点时遇到了一个错误。 My result is close to the expected, but not close enough:
我的结果接近预期,但不够接近:
posA = {47.64570362, -122.14073746}
posB = {47.64316917, -122.14032175}
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}
预期结果(取自可移动型计算器)= 47°38'40“N,122°08'26”W =
{47.644444, -122.140556}
我的结果: {49.6054801645915, -122.14052959995759}
Here is my 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));
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. 我有几种私有方法可以在Degrees和Radians之间进行转换。 Eg
例如
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. 我无法弄清楚为什么我的结果在Lat值上偏离了几度。 Any ideas?
有任何想法吗?
Thanks 谢谢
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;
}
Some methods (eg WGS84 conventions) include oblateness of the earth. 一些方法(例如WGS84惯例)包括地球的扁率。 ( at least that's what it says here )
( 至少这就是它在这里所说的 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.