Calculate distance between 2 GPS coordinates

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

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

相关标签:
29条回答
  • 2020-11-21 23:49

    // Maybe a typo error ?
    We have an unused variable dlon in GetDirection,
    I assume

    double y = Math.Sin(dlon) * Math.Cos(lat2);
    // cannot use degrees in Cos ?
    

    should be

    double y = Math.Sin(dlon) * Math.Cos(dlat);
    
    0 讨论(0)
  • 2020-11-21 23:49

    I think a version of the algorithm in R is still missing:

    gpsdistance<-function(lat1,lon1,lat2,lon2){
    
    # internal function to change deg to rad
    
    degreesToRadians<- function (degrees) {
    return (degrees * pi / 180)
    }
    
    R<-6371e3  #radius of Earth in meters
    
    phi1<-degreesToRadians(lat1) # latitude 1
    phi2<-degreesToRadians(lat2) # latitude 2
    lambda1<-degreesToRadians(lon1) # longitude 1
    lambda2<-degreesToRadians(lon2) # longitude 2
    
    delta_phi<-phi1-phi2 # latitude-distance
    delta_lambda<-lambda1-lambda2 # longitude-distance
    
    a<-sin(delta_phi/2)*sin(delta_phi/2)+
    cos(phi1)*cos(phi2)*sin(delta_lambda/2)*
    sin(delta_lambda/2)
    
    cc<-2*atan2(sqrt(a),sqrt(1-a))
    
    distance<- R * cc
    
    return(distance)  # in meters
    }
    
    0 讨论(0)
  • 2020-11-21 23:52

    If you need something more accurate then have a look at this.

    Vincenty's formulae are two related iterative methods used in geodesy to calculate the distance between two points on the surface of a spheroid, developed by Thaddeus Vincenty (1975a) They are based on the assumption that the figure of the Earth is an oblate spheroid, and hence are more accurate than methods such as great-circle distance which assume a spherical Earth.

    The first (direct) method computes the location of a point which is a given distance and azimuth (direction) from another point. The second (inverse) method computes the geographical distance and azimuth between two given points. They have been widely used in geodesy because they are accurate to within 0.5 mm (0.020″) on the Earth ellipsoid.

    0 讨论(0)
  • 2020-11-21 23:52

    If you're using .NET don't reivent the wheel. See System.Device.Location. Credit to fnx in the comments in another answer.

    using System.Device.Location;
    
    double lat1 = 45.421527862548828D;
    double long1 = -75.697189331054688D;
    double lat2 = 53.64135D;
    double long2 = -113.59273D;
    
    GeoCoordinate geo1 = new GeoCoordinate(lat1, long1);
    GeoCoordinate geo2 = new GeoCoordinate(lat2, long2);
    
    double distance = geo1.GetDistanceTo(geo2);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 23:54

    I guess you want it along the curvature of the earth. Your two points and the center of the earth are on a plane. The center of the earth is the center of a circle on that plane and the two points are (roughly) on the perimeter of that circle. From that you can calculate the distance by finding out what the angle from one point to the other is.

    If the points are not the same heights, or if you need to take into account that the earth is not a perfect sphere it gets a little more difficult.

    0 讨论(0)
提交回复
热议问题