具体实现
头文件:
#ifndef COORDCONVERTDISTANCE_H
#define COORDCONVERTDISTANCE_H
#include <QObject>
#include "ExtendStructure\Include\Vector3D.h"
class CCoordConvertDistance
{
public:
CCoordConvertDistance();
~CCoordConvertDistance();
public:
/**
* @fn ConvertFunc
* @brief 根据两点坐标计算两点平面距离
* @param vMinpoint第一点, vMaxpoint第二点
* @return
*/
void ConvertFunc(const CVector3D& vMinpoint, const CVector3D& vMaxpoint);
/**
* @fn GetDistance
* @brief 获取函数ConvertFunc计算的平面距离
* @param
* @return double m_dDistance
*/
double GetDistance();
private:
//两点间距离
double m_dDistance;
//两点间横向距离未实现
double m_dYDistance;
//两点间纵向距离未实现
double m_dXDistance;
};
#endif // COORDCONVERTDISTANCE_H
源文件:
#include "CoordConvertDistance.h"
#include "math.h"
#include "qmath.h"
#define _USE_MATH_DEFINES
#define R 6371004
CCoordConvertDistance::CCoordConvertDistance()
{
}
CCoordConvertDistance::~CCoordConvertDistance()
{
}
void CCoordConvertDistance::ConvertFunc(const CVector3D& m_vPoint1, const CVector3D& m_vPoint2)
{
//角度转弧度
double dLon1 = m_vPoint1.x;
double dLat1 = m_vPoint1.y;
double dLon2 = m_vPoint2.x;
double dLat2 = m_vPoint2.y;
double dRadLon1 = (dLon1 * M_PI/180.0);
double dRadLat1 = (dLat1 * M_PI/180.0);
double dRadLon2 = (dLon2 * M_PI/180.0);
double dRadLat2 = (dLat2 * M_PI/180.0);
//纬度的差值
double dLatRange = dRadLat2-dRadLat1;
//经度差值
double dLonRange = dRadLon2-dRadLon1;
//弧度长度
double ds=2 * qAsin(sqrt(pow(qSin(dLatRange/2), 2) + qCos(dRadLat1) * qCos(dRadLat2) * pow(qSin(dLonRange/2), 2)));
//获取长度
m_dDistance = ds * R;
//返回最接近参数的 long。结果将舍入为整数:加上 1/2
//s=Math.round(s*10000)/10000;
}
double CCoordConvertDistance::GetDistance()
{
return m_dDistance;
}
总结
注:三角函数需要使用弧度,需要将经纬度的角度制转为弧度制。