Advertisement
IzaacJ

Correct way to calculate distance in meters?

May 7th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. // The calculation below is based upon a calculation found on StackOverflow:
  2. // http://stackoverflow.com/a/11172685
  3.  
  4. // This code runs on an Azure WebService and I have found no way to use GeoCoordinate to access the GetDistanceTo method,
  5. // which is why I've tried to solve it this way.
  6.  
  7. // Calculation starts here
  8. var R = 6378.137; // Radius of Earth in KM
  9. var dLat = (Marker.Lat - Lat) * Math.PI / 180;
  10. var dLng = (Marker.Lng - Long) * Math.PI / 180;
  11. var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
  12.         Math.Cos(Marker.Lat * Math.PI / 180) * Math.Cos(Lat * Math.PI / 180) *
  13.         Math.Sin(dLng / 2) * Math.Sin(dLng / 2);
  14. var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
  15. var d = R * c * 1000; // d = the distance in meters;
  16. // Calculation ends here
  17.  
  18. if (d <= Distance) // if the calculated distance (d) is less or equal to Distance it should be included in the results.
  19. {
  20.    FinalMarkers.Add(Marker);
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement