关于经纬度计算的代码

1、计算两个经纬度间的距离

private const double EARTH_RADIUS = 6378.137;
private static double rad(double d)
{
   return d * Math.PI / 180.0;
}

public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
   double radLat1 = rad(lat1);
   double radLat2 = rad(lat2);
   double a = radLat1 - radLat2;
   double b = rad(lng1) - rad(lng2);
   double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a/2),2) +
    Math.Cos(radLat1)*Math.Cos(radLat2)*Math.Pow(Math.Sin(b/2),2)));
   s = s * EARTH_RADIUS;
   s = Math.Round(s * 10000) / 10000;
   return s;
}

 

/**** 根据两站点的经纬度求两站点间的距离 ****/
double D_jw(double wd1,double jd1,double wd2,double jd2)
{
    double x,y,out;
    double PI=3.14159265;
    double R=6.371229*1e6;

    x=(jd2-jd1)*PI*R*cos( ((wd1+wd2)/2) *PI/180)/180;
    y=(wd2-wd1)*PI*R/180;
    out=hypot(x,y);
    return out/1000;
}

 


3  一个经纬度相关计算的C++类
写了一个经纬度距离计算的类

 

--------------CJWD.h--------------

#ifndef __JWD_AND_HELPER_20051005
#define __JWD_AND_HELPER_20051005

#include "stdafx.h"
#include <math.h>
#include <iostream>
using namespace std;

#ifndef PI

#define PI 3.14159265;

#endif
static double Rc = 6378137;  // 赤道半径

static double Rj = 6356725;  // 极半径

namespace CDYW{

 


class JWD
{
public:
double m_LoDeg, m_LoMin, m_LoSec;  // longtitude 经度
double m_LaDeg, m_LaMin, m_LaSec;
double m_Longitude, m_Latitude;
double m_RadLo, m_RadLa;
double Ec;
double Ed;
public:
// 构造函数, 经度: loDeg 度, loMin 分, loSec 秒;  纬度: laDeg 度, laMin 分, laSec秒
    JWD(double loDeg, double loMin, double loSec, double laDeg, double laMin, double laSec)
{
  m_LoDeg=loDeg; m_LoMin=loMin; m_LoSec=loSec; m_LaDeg=laDeg; m_LaMin=laMin; m_LaSec=laSec;
  m_Longitude = m_LoDeg + m_LoMin / 60 + m_LoSec / 3600;
  m_Latitude = m_LaDeg + m_LaMin / 60 + m_LaSec / 3600;
  m_RadLo  = m_Longitude * PI / 180.;
  m_RadLa  = m_Latitude * PI / 180.;
  Ec = Rj + (Rc - Rj) * (90.- m_Latitude) / 90.;
  Ed = Ec * cos(m_RadLa);
}

//!
JWD(double longitude, double latitude)
{
  m_LoDeg = int(longitude);
  m_LoMin = int((longitude - m_LoDeg)*60);
  m_LoSec = (longitude - m_LoDeg - m_LoMin/60.)*3600;
 
  m_LaDeg = int(latitude);
  m_LaMin = int((latitude - m_LaDeg)*60);
  m_LaSec = (latitude - m_LaDeg - m_LaMin/60.)*3600;
 
  m_Longitude = longitude;
  m_Latitude = latitude;
  m_RadLo = longitude * PI/180.;
  m_RadLa = latitude * PI/180.;
  Ec = Rj + (Rc - Rj) * (90.-m_Latitude) / 90.;
  Ed = Ec * cos(m_RadLa);
}
};

 

class CJWDHelper
{
public:
CJWDHelper() {};
~CJWDHelper() {};

 

4 //! 计算点A 和 点B的经纬度,求他们的距离和点B相对于点A的方位
 

/*!
  * /param A A点经纬度
  * /param B B点经纬度
  * /param angle B相对于A的方位, 不需要返回该值,则将其设为空
  * /return A点B点的距离
  */
static double distance(JWD A, JWD B, double *angle)
{
  double dx = (B.m_RadLo - A.m_RadLo) * A.Ed;
  double dy = (B.m_RadLa - A.m_RadLa) * A.Ec;
  double out = sqrt(dx * dx + dy * dy);
 
  if( angle != NULL)
  {
   *angle = atan(fabs(dx/dy))*180./PI;
   // 判断象限
   double dLo = B.m_Longitude - A.m_Longitude;
   double dLa = B.m_Latitude - A.m_Latitude;
  
   if(dLo > 0 && dLa <= 0) {
     *angle = (90. - *angle) + 90.;
    }
   else if(dLo <= 0 && dLa < 0) {
     *angle = *angle + 180.;
    }
   else if(dLo < 0 && dLa >= 0) {
     *angle = (90. - *angle) + 270;
    }
}

 

return out/1000;
}

 

5 //! 计算点A 和 点B的经纬度,求他们的距离和点B相对于点A的方位
 

/*!
  * /param longitude1 A点经度
  * /param latitude1 A点纬度
  * /param longitude2 B点经度
  * /param latitude2 B点纬度
  * /param angle B相对于A的方位, 不需要返回该值,则将其设为空
  * /return A点B点的距离
  */
static double distance(
  double longitude1, double latitude1,
  double longitude2, double latitude2,
  double *angle)
{
  JWD A(longitude1,latitude1);
  JWD B(longitude2,latitude2);

 

  return distance(A, B, angle);
}

 

6 //! 已知点A经纬度,根据B点据A点的距离,和方位,求B点的经纬度
 

/*!
  * /param A 已知点A
  * /param distance B点到A点的距离
  * /param angle B点相对于A点的方位
  * /return B点的经纬度坐标
  */
static JWD GetJWDB(JWD A, double distance, double angle)
{
  double dx = distance*1000 * sin(angle * PI /180.);
  double dy = distance*1000 * cos(angle * PI /180.);
 
  //double dx = (B.m_RadLo - A.m_RadLo) * A.Ed;
  //double dy = (B.m_RadLa - A.m_RadLa) * A.Ec;

 

  double BJD = (dx/A.Ed + A.m_RadLo) * 180./PI;
  double BWD = (dy/A.Ec + A.m_RadLa) * 180./PI;
  JWD B(BJD, BWD);
  return B;
}

 

7 //! 已知点A经纬度,根据B点据A点的距离,和方位,求B点的经纬度
8 =========== 测试程序==========

/*!
  * /param longitude 已知点A经度
  * /param latitude 已知点A纬度
  * /param distance B点到A点的距离
  * /param angle B点相对于A点的方位
  * /return B点的经纬度坐标
  */
static JWD GetJWDB(double longitude, double latitude, double distance, double angle)
{
  JWD A(longitude,latitude);
  return GetJWDB(A, distance, angle);
}

};
}
#endif

 

#include "stdafx.h"
#include <math.h>
#include <iostream>#include "CJWD.h"
using namespace std;using namespace CDYW;
double Rc = 6378137;  // 赤道半径
double Rj = 6356725;  // 极半径// 绵阳
double jd1 = 104.740999999;
double wd1 = 31.4337;// 成都
double jd2 = 104.01;
double wd2 = 30.40; int main(int argc, char* argv[])
{
double angle = 0;
cout << "A(绵阳): JD = " << jd1 << "  WD = " << wd1 << endl;
cout << "B(成都): JD = " << jd2 << "  WD = " << wd2 << endl;
cout << "--------------------" << endl;
cout << D_jw(wd1,jd1,wd2,jd2, angle) << endl;
cout << "angle: " << angle <<endl;
cout << "==============" <<endl;
JWD A(jd1,wd1),B(jd2,wd2);
double distance = CJWDHelper::distance(jd1,wd1,jd2,wd2, &angle);
//cout << CJWDHelper::distance(A,B, &angle) << endl;
cout << distance << endl;
cout << "angle: " << angle <<endl;
cout << "==============" <<endl;
JWD C = CJWDHelper::GetJWDB(A, distance, angle);
cout << "JD = " << C.m_Longitude << "  WD = " << C.m_Latitude << endl;
cout << "==============" <<endl;
cout << A.m_LoDeg << " " << A.m_LoMin << " " << A.m_LoSec << endl; return 0;
}

 

 
 
2 根据两站点的经纬度求两站点间的距离
==
根据经纬度计算方位的C代码如下所示: ```c #include <stdio.h> #include <math.h> double calculateBearing(double lat1, double lon1, double lat2, double lon2) { double radianLat1 = lat1 * M_PI / 180.0; double radianLat2 = lat2 * M_PI / 180.0; double radianDeltaLon = (lon2 - lon1) * M_PI / 180.0; double y = sin(radianDeltaLon) * cos(radianLat2); double x = cos(radianLat1) * sin(radianLat2) - sin(radianLat1) * cos(radianLat2) * cos(radianDeltaLon); double bearing = atan2(y, x) * 180.0 / M_PI; if (bearing < 0) { bearing += 360.0; } return bearing; } int main() { double lat1, lon1, lat2, lon2; printf("请输入起始点的纬度和经度(以空格分隔):"); scanf("%lf %lf", &lat1, &lon1); printf("请输入目标点的纬度和经度(以空格分隔):"); scanf("%lf %lf", &lat2, &lon2); double bearing = calculateBearing(lat1, lon1, lat2, lon2); printf("起始点指向目标点的方位为:%lf\n", bearing); return 0; } ``` 该代码首先定义了一个函数`calculateBearing`,用于计算给定两个坐标点的方位角(bearing),其中`lat1`和`lon1`为起始点的纬度和经度,`lat2`和`lon2`为目标点的纬度和经度。函数内部将角度转化为弧度,并根据经纬度差进行一系列计算,最终得到方位角。如果方位角为负值,则将其加上360度,确保方位角的范围在0~360度之间。 在`main`函数中,通过用户输入起始点和目标点的经纬度,调用`calculateBearing`函数计算方位角,并输出结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值