locked
How to find distance between two points on earth RRS feed

  • Question

  • hi All,

    I want to calculate the distance (in meters) between two points whose latitude and longitude will be entered by the user...
    Need this for GPS-based app in win mo 6.0

    For ex :-

    Double distInMet = calDistInMet (Double lat1, Double lat2, Double lon1, Double lon2);

    Is thr any google api for this ?
    If not, can anyone help me out what will be the code inside the body of the above method ?

    Thanks in advance,
    Ankur
    Saturday, August 8, 2009 12:10 PM

Answers


  • I don't know if it is in the google API, but there's a code sample in my power management article ( http://www.codeproject.com/KB/mobile/WiMoPower1.aspx ) called GPS Perimeter that contained a class for this.

    using System;
    
    using System.Collections.Generic;
    using System.Text;
    
    namespace GpsPerimeter
    {
        public static class DistanceCalculatoor
        {
            public const double EarthRadiusInMiles = 3956.0;
            public const double EarthRadiusInKilometers = 6367.0;
            public static double ToRadian(double val) { return val * (Math.PI / 180); }
            public static double ToDegree(double val) { return val * 180 / Math.PI; }
            public static double DiffRadian(double val1, double val2) { return ToRadian(val2) - ToRadian(val1); }
    
            /// <summary> 
            /// Calculate the distance between two geocodes. Defaults to using Miles. 
            /// </summary> 
    
            /// <summary> 
            /// Calculate the distance between two geocodes. 
            /// </summary> 
            public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, double radius)
            {
    
                return radius * 2 * Math.Asin(Math.Min(1, Math.Sqrt((Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0)))));
            }
    
            public static Double Bearing(double lat1, double lng1, double lat2, double lng2)
            {
    
                {
                    var dLat = lat2 - lat2;
                    var dLon = lng2 - lng1;
                    var dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
                    var q = (Math.Abs(dLat) > 0) ? dLat / dPhi : Math.Cos(lat1);
                    
                    if (Math.Abs(dLon) > Math.PI)
                    {
                        dLon = dLon > 0 ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon);
                    }
                    //var d = Math.Sqrt(dLat * dLat + q * q * dLon * dLon) * R;
                    var brng = ToDegree(Math.Atan2(dLon, dPhi));
                    return brng;
                }
    
            }
        }
    
    
    }
    

    Joel Ivory Johnson
    Saturday, August 8, 2009 12:39 PM

All replies


  • I don't know if it is in the google API, but there's a code sample in my power management article ( http://www.codeproject.com/KB/mobile/WiMoPower1.aspx ) called GPS Perimeter that contained a class for this.

    using System;
    
    using System.Collections.Generic;
    using System.Text;
    
    namespace GpsPerimeter
    {
        public static class DistanceCalculatoor
        {
            public const double EarthRadiusInMiles = 3956.0;
            public const double EarthRadiusInKilometers = 6367.0;
            public static double ToRadian(double val) { return val * (Math.PI / 180); }
            public static double ToDegree(double val) { return val * 180 / Math.PI; }
            public static double DiffRadian(double val1, double val2) { return ToRadian(val2) - ToRadian(val1); }
    
            /// <summary> 
            /// Calculate the distance between two geocodes. Defaults to using Miles. 
            /// </summary> 
    
            /// <summary> 
            /// Calculate the distance between two geocodes. 
            /// </summary> 
            public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, double radius)
            {
    
                return radius * 2 * Math.Asin(Math.Min(1, Math.Sqrt((Math.Pow(Math.Sin((DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.Cos(ToRadian(lat1)) * Math.Cos(ToRadian(lat2)) * Math.Pow(Math.Sin((DiffRadian(lng1, lng2)) / 2.0), 2.0)))));
            }
    
            public static Double Bearing(double lat1, double lng1, double lat2, double lng2)
            {
    
                {
                    var dLat = lat2 - lat2;
                    var dLon = lng2 - lng1;
                    var dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
                    var q = (Math.Abs(dLat) > 0) ? dLat / dPhi : Math.Cos(lat1);
                    
                    if (Math.Abs(dLon) > Math.PI)
                    {
                        dLon = dLon > 0 ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon);
                    }
                    //var d = Math.Sqrt(dLat * dLat + q * q * dLon * dLon) * R;
                    var brng = ToDegree(Math.Atan2(dLon, dPhi));
                    return brng;
                }
    
            }
        }
    
    
    }
    

    Joel Ivory Johnson
    Saturday, August 8, 2009 12:39 PM
  • hi Joel,

    Thank you for the gr8 help. I got my work done...

    Thanks,
    Ankur
    Monday, August 10, 2009 9:41 AM