在C++的 Boost.Geometry、GDAL/OGR 和 GeographicLib。这些库都可以用于计算两个经纬度点之间的地面距离。
. Boost.Geometry
-
描述:Boost库的一部分,提供了几何计算功能,包括计算两点之间的地面距离。
-
优势:轻量级、易于集成到C++项目中,使用方便。
-
缺点:精度不够
GDAL/OGR
-
描述:广泛用于GIS应用,支持各种地理数据操作和计算。
-
优势:功能强大,适合处理复杂地理信息和精度要求高的计算。
-
缺点:复杂
GeographicLib
-
描述:提供精确的地理计算,包括大圆距离和方向计算。
-
优势:高精度,特别适用于长距离和椭球体模型计算。
感谢博客 园的windows+VS2022安装GeographicLib1.52 - MorpheusDong - 博客园
这篇帖子,这篇帖子是以上面的帖子为基础写的。
GeographicLib是一个开源的C++库,主要用于高精度地理计算。它提供了一系列工具和算法,能够进行地球表面距离、方向、面积和其他地理测量的计算。
首先在官网上下载 相关的exe安装文件
GeographicLib download | SourceForge.net

下载完成后,
一路确定

这个是选择是否把路径加到环境变量里,加不加都行。
选择目标文件夹

安装完成后的目录如下。

配置VS
右击项目->属性->【VC++目录】。


在"包含目录"中,添加 \include 目录。

在"库目录"中,添加 \lib 目录。

\lib目录,带_d
后缀的是debug模式要链接的依赖。

在【链接器】->【输入】->【附加依赖项】,包含:
Geographic_d.lib
Geographic_d-i.lib
后注意要添加一个预处理器定义。
GEOGRAPHICLIB_SHARED_LIB=1
指定使用共享库(shared library)版本的GeographicLib

确定保存,至此代码可以编译,但是还要配置一下环境变量,否则会提示缺少dll文件。
配置环境变量
打开环境变量窗口,在"系统变量"的path里添加\bin目录。


点击后
双击path

把dll文件所在的bin文件夹地址写入

重启VS,新建项目,输入测试代码

#include <iostream>
#include <GeographicLib/Geodesic.hpp> // 确保包含了正确的头文件
double calculate_geodesic_distance(double lat1, double lon1, double lat2, double lon2) {
const GeographicLib::Geodesic& geod = GeographicLib::Geodesic::WGS84();
double distance;
geod.Inverse(lat1, lon1, lat2, lon2, distance);
return distance / 1000.0; // 转换为公里
}
int main() {
double distance = calculate_geodesic_distance(34.0522, -118.2437, 40.7128, -74.0060);
std::cout << "Geodesic Distance: " << distance << " km" << std::endl;
return 0;
}
点击调试运行

计算得到洛杉矶到纽约的距离为3944.42km