Calculate distance between 2 GPS coordinates

前端 未结 29 3569
青春惊慌失措
青春惊慌失措 2020-11-21 23:34

How do I calculate distance between two GPS coordinates (using latitude and longitude)?

29条回答
  •  臣服心动
    2020-11-21 23:53

    C# Version of Haversine

    double _eQuatorialEarthRadius = 6378.1370D;
    double _d2r = (Math.PI / 180D);
    
    private int HaversineInM(double lat1, double long1, double lat2, double long2)
    {
        return (int)(1000D * HaversineInKM(lat1, long1, lat2, long2));
    }
    
    private double HaversineInKM(double lat1, double long1, double lat2, double long2)
    {
        double dlong = (long2 - long1) * _d2r;
        double dlat = (lat2 - lat1) * _d2r;
        double a = Math.Pow(Math.Sin(dlat / 2D), 2D) + Math.Cos(lat1 * _d2r) * Math.Cos(lat2 * _d2r) * Math.Pow(Math.Sin(dlong / 2D), 2D);
        double c = 2D * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1D - a));
        double d = _eQuatorialEarthRadius * c;
    
        return d;
    }
    

    Here's a .NET Fiddle of this, so you can test it out with your own Lat/Longs.

提交回复
热议问题