osg根据经纬度计算两点之间的距离
时间: 2023-08-31 09:05:20 浏览: 81
根据经纬度计算两点之间的距离
在OpenSceneGraph中,可以使用osgEarth::GeoMath类来计算经纬度坐标系下两点之间的距离。具体步骤如下:
1. 导入osgEarth/GeoMath头文件:
```
#include <osgEarth/GeoMath>
```
2. 定义两个经纬度坐标点:
```
osgEarth::GeoPoint point1(osgEarth::SpatialReference::create("wgs84"), lon1, lat1);
osgEarth::GeoPoint point2(osgEarth::SpatialReference::create("wgs84"), lon2, lat2);
```
其中,lon1、lat1、lon2、lat2分别表示两个点的经度和纬度。
3. 调用GeoMath::distance函数计算两点之间的距离,并将结果转换为指定单位:
```
double distance = osgEarth::GeoMath::distance(point1, point2, osgEarth::Units::KILOMETERS);
```
其中,第三个参数指定了距离单位,这里使用公里(KILOMETERS)。
完整代码示例:
```
#include <osgEarth/GeoMath>
#include <iostream>
int main()
{
double lon1 = 116.39, lat1 = 39.91;
double lon2 = 121.47, lat2 = 31.23;
osgEarth::GeoPoint point1(osgEarth::SpatialReference::create("wgs84"), lon1, lat1);
osgEarth::GeoPoint point2(osgEarth::SpatialReference::create("wgs84"), lon2, lat2);
double distance = osgEarth::GeoMath::distance(point1, point2, osgEarth::Units::KILOMETERS);
std::cout << "The distance between point1 and point2 is " << distance << " kilometers." << std::endl;
return 0;
}
```
阅读全文