Calculating the distance between 2 latitudes and longitudes that are saved in a text file?

前端 未结 4 598
悲哀的现实
悲哀的现实 2020-12-16 04:10

I\'ve looked around and still couldn\'t find anything to help me really! I\'ve written a program to calculate the distance between 2 cities using their Latitudes and Longitu

相关标签:
4条回答
  • 2020-12-16 04:45

    Using boost.geometry

    typedef boost::geometry::model::point<
        double, 2, boost::geometry::cs::spherical_equatorial<boost::geometry::degree>
    > spherical_point;
    
    spherical_point p(lon1_degree, lat1_degree);
    spherical_point q(lon2_degree, lat2_degree);
    double dist = boost::geometry::distance(p, q);
    double const earth_radius = 6371.0; // Km
    double dist_km = dist*earth_radius;
    
    0 讨论(0)
  • 2020-12-16 04:48

    As it is said, Haversine formula is your answer:

    #include <math.h>
    #include <cmath> 
    #define earthRadiusKm 6371.0
    
    // This function converts decimal degrees to radians
    double deg2rad(double deg) {
      return (deg * M_PI / 180);
    }
    
    //  This function converts radians to decimal degrees
    double rad2deg(double rad) {
      return (rad * 180 / M_PI);
    }
    
    /**
     * Returns the distance between two points on the Earth.
     * Direct translation from http://en.wikipedia.org/wiki/Haversine_formula
     * @param lat1d Latitude of the first point in degrees
     * @param lon1d Longitude of the first point in degrees
     * @param lat2d Latitude of the second point in degrees
     * @param lon2d Longitude of the second point in degrees
     * @return The distance between the two points in kilometers
     */
    double distanceEarth(double lat1d, double lon1d, double lat2d, double lon2d) {
      double lat1r, lon1r, lat2r, lon2r, u, v;
      lat1r = deg2rad(lat1d);
      lon1r = deg2rad(lon1d);
      lat2r = deg2rad(lat2d);
      lon2r = deg2rad(lon2d);
      u = sin((lat2r - lat1r)/2);
      v = sin((lon2r - lon1r)/2);
      return 2.0 * earthRadiusKm * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
    }
    
    0 讨论(0)
  • 2020-12-16 05:04

    This is the method that I would use for finding the distance This is probably easier

    Or this, not concidering the "bend" of the Earthenter image description here

    0 讨论(0)
  • 2020-12-16 05:10

    for people who need in swift:

      // Haversine formula:
    
        func deg2rad(_ deg: Double) ->Double {
            return deg * Double.pi  / 180.0
        }
    
        func distanceEarth(lat1d: Double, lon1d: Double, lat2d: Double, lon2d: Double) ->Double {
        let  earthRadiusKm = 6371.0
    
        let lat1r = deg2rad(lat1d);
        let lon1r = deg2rad(lon1d);
        let lat2r = deg2rad(lat2d);
        let lon2r = deg2rad(lon2d);
        let u = sin((lat2r - lat1r)/2);
        let v = sin((lon2r - lon1r)/2);
        return 2.0 * earthRadiusKm * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
    }
    
    //test here.... https://andrew.hedges.name/experiments/haversine/
    
    
    func doTestHaversine(){
    
        let km = distanceEarth(lat1d: 38.898556, lon1d: -77.037852, lat2d: 38.897147, lon2d: -77.043934)
         print(km)  // should show : 0.549 or similar..
    }
    
    0 讨论(0)
提交回复
热议问题