Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

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

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 Longitudes, the cities details are saved in a file then loaded in my program into a BST! So far everything works fine except when i run the codes that is suppose to calculate the distance i get the same answer for every cities! Am not quite sure as to why i keep getting the same answer for every cities! Please help point me into the right direction?

here are the codes to calculate the distance

#include <cmath> 
#define pi 3.14159265358979323846

string userResponse;
float globalLat1, globalLon1, globalLat2, globalLon2;

for(int j= 0; j < 2; j++){
        string whatever;
        if (j==0){
          bool hasbeenfound = false;
           do{
                //ask the user to enter their first city of their choice
                 whatever = "first ";
                  cout << "Enter your " + whatever + "City" << endl;
                  cout << "-------------------" << endl;
                  cin >> userResponse;
                  cout << endl;
                  if (Cities->search(userResponse)) //check if the entered city already exist
                  {
                  hasbeenfound = true;
                  }
                  else{
                       cout << "City not Found" << endl;
                       cout << endl;
                       }
                  //globalCity1 = Cities->sRootName;
                  globalLat1 = Cities->sLatitude;
                  globalLon1 = Cities->sLongitude;
                  }
                  while(hasbeenfound == false); //while the entered city hasn't been found, repeat the process

               }else
               {
                   bool hasbeenfound = false;
                    do{
                        //ask the user to enter their second city of their choice
                              whatever = "second ";
                              cout << endl;
                              cout << "Enter your " + whatever + "City" << endl;
                              cout << "-------------------" << endl;
                              cin >> userResponse;
                              cout << endl;
                              if (Cities->search(userResponse)) //check if the entered city already exist
                              {
                              hasbeenfound = true;
                              }
                              else{
                                   cout << "City not Found" << endl;
                                   }
                              //globalCity2 = Cities->sRootName;
                              globalLat2 = Cities->sLatitude;
                              globalLon2 = Cities->sLongitude;
                              }
                    while(hasbeenfound == false); //while the entered city hasn't been found, repeat the process

                       }
                    }

// This function converts decimal degrees to radians
double deg2rad(double deg) {
return (deg * pi / 180);
};

//  This function converts radians to decimal degrees
double rad2deg(double rad) {
return (rad * 180 / pi);
};

//distance calculations
cout << endl;
distan = sin(globalLat1)) * sin(deg2rad(globalLat2)) + cos(deg2rad(globalLat1)) * cos(deg2rad(globalLat2)) * cos(globalLon2 - globalLon1);
distan = rad2deg(distan);
distan = distan * 60 * 1.1515;
distan = (6371 * pi * distan)/180;
cout << "The Distance between the to cities is: " << distan << " kilometers" << endl;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...