计算保存在文本文件中的 2 个纬度和经度之间的距离?
Posted
技术标签:
【中文标题】计算保存在文本文件中的 2 个纬度和经度之间的距离?【英文标题】:Calculating the distance between 2 latitudes and longitudes that are saved in a text file? 【发布时间】:2012-04-17 20:53:25 【问题描述】:我环顾四周,仍然找不到任何真正可以帮助我的东西! 我编写了一个程序来使用它们的纬度和经度计算两个城市之间的距离,城市的详细信息保存在一个文件中,然后在我的程序中加载到 BST 中! 到目前为止,一切正常,除了当我运行假设计算距离的代码时,我得到每个城市的相同答案! 我不太确定为什么我在每个城市都得到相同的答案! 请帮我指出正确的方向?
这里是计算距离的代码
#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;
【问题讨论】:
缺少最重要的部分...您如何填充globalLat/Lon/1/2
。
请问您所说的“填充”是什么意思?
@Kap 填充它们的代码很重要,因为这几乎肯定是错误所在。 (除了公式中的小故障。)
@Kap:尝试使用一些printf()
调试:在计算distan
之后添加printf("distan(%f, %f, %f, %f) == %f\n", globalLat1, globalLat2, globalLon1, globalLon2, distan);
,看看输出是否显示任何有趣的东西。
@RobKennedy - 这些是唯一应该让城市 Lonand Lat 然后进行计算的代码。 Cities->search
在我运行程序时确实找到了城市!我刚刚检查过,正如你提到的,似乎纬度和经度没有像我希望的那样被存储。感谢您为我指出这一点!
【参考方案1】:
正如所说,Haversine 公式就是您的答案:
#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));
【讨论】:
这是正确答案,结果也与用javascript编写的Online Distance Calculator匹配。 你也可以通过添加几行来获得支持吗?谢谢 您的答案中未使用 rad2Deg【参考方案2】:使用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;
【讨论】:
【参考方案3】:对于需要 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..
【讨论】:
【参考方案4】:这是我用来寻找距离的方法
或者这个,不考虑地球的“弯曲”
【讨论】:
是减法、乘法、加法和平方根,Kap。 “那个”的哪一部分你需要知道怎么做? 这是欧几里得距离的公式,它不适用于地球,因为它不是线性的。纬度/经度空间不是平面。 @Spidey:你永远无法说服那些扁平地球人。 你没有 x,y,z。你有纬度和经度,一个坐标系,其中一个坐标不平行,在一个球形表面上启动。你的公式都不起作用。 @Kap 首先,地球是一种球形,其次,平行线在球面上是弯曲的,因为它们不是两点之间的最短线(直线的定义)。经络是。一个有用的公式必须考虑到这一点。当距离不太大时,上面的第一个公式可能是一个有用的近似值。以上是关于计算保存在文本文件中的 2 个纬度和经度之间的距离?的主要内容,如果未能解决你的问题,请参考以下文章