I need to find the midpoint between two Latitude/Longitude points.
I am trying to center a map between two locations I have plotted and need to find that point.
I have written this function from other examples I have found and it doesn't seem to work.
public static void midPoint(double lat1, double lon1, double lat2, double lon2, out double lat3_OUT, out double lon3_OUT)
{
//http:// stackoverflow.com/questions/4656802/midpoint-between-two-latitude-and-longitude
double dLon = DistanceAlgorithm.Radians(lon2 - lon1);
//convert to radians
lat1 = DistanceAlgorithm.Radians(lat1);
lat2 = DistanceAlgorithm.Radians(lat2);
lon1 = DistanceAlgorithm.Radians(lon1);
double Bx = Math.Cos(lat2) * Math.Cos(dLon);
double By = Math.Cos(lat2) * Math.Sin(dLon);
double lat3 = Math.Atan2(Math.Sin(lat1) + Math.Sin(lat2), Math.Sqrt((Math.Cos(lat1) + Bx) * (Math.Cos(lat1) + Bx) + By * By));
double lon3 = lon1 + Math.Atan2(By, Math.Cos(lat1) + Bx);
lat3_OUT = lat3;
lon3_OUT = lon3;
}
Here is an example output.
lat1 37.7977008
lat2 37.798392
lon1 -122.1637914
lon2 -122.161464
lat3 0.65970036060147585
lon3 -2.1321400763480485
See Lat3 and Lon3.
Thanks!
It appears that your output is correct, except that it is in radians and you are expecting degrees? you may need to convert from radian to degrees to meet the expected output.
-(2.1321400763480485 radians) = -122.162628 degrees
0.65970036060147585 radians = 37.7980464 degrees
It looks like you are already using the great-circle algorithm that Jon Martin and IanDotKelly mentioned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With