Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

c# - 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. 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}

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. 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?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...