计算一个经纬度在另一个经纬度的方位
double toRadian(double angdeg)
{
return angdeg / 180.0 * PI;
}
double toDegree(double angrad)
{
return angrad * 180.0 / PI;
}
//获取方位
double GetBear(double lat, double lon, double lat1, double lon1)
{
double longitude1 = lon;
double longitude2 = lon1;
double latitude1 = toRadian(lat);
double latitude2 = toRadian(lat1);
double longDiff = toRadian(longitude2 - longitude1);
double y = sin(longDiff) * cos(latitude2);
double x = cos(latitude1) * sin(latitude2) - sin(latitude1) * cos(latitude2) * cos(longDiff);
double bear= toDegree(atan2(y, x));
if(bear < 0)
{
bear += 360;
}
else if(bear > 360)
{
bear -= 360;
}
return bear;
}
GetBear函数接受一个起点经纬度位置跟目标点经纬度,返回一个角度。