两经纬度之间的距离计算

以下是计算两经纬度之间距离的代码,分为:头文件、源代码和测试代码三部分。
具体如下:

 1 // LatLonDistanceDlg.h : 头文件
 2 //
 3
 4 #pragma once
 5
 6 typedef struct
 7 {
 8     double dLongitude;
 9     double dLatitude;
10 }MyLatLong_T,*pMyLatLong_T;
11
12 // CLatLonDistanceDlg 对话框
13 class CLatLonDistanceDlg : public CDialog
14 {
15     ......
16
17     void DistanceOfTwoPositions(MyLatLong_T ll_PosStart, MyLatLong_T ll_PosEnd, double &dis);
18 // 实现
19 protected:
20     ......
21 };
22
23 // LatLonDistanceDlg.cpp : 实现文件
24 //
25
26 // 常量定义
27 const double gConstPI = 3.1415926536;
28 // WGS-84 长轴半径
29 #define EARTH_LONG_RADIUS_WGS84 6378137.0       // 单位: 米
30 // WGS-84 地球扁率(earth flattening)
31 #define EARTH_FLATTENING_WGS84  298.257223563
32 // meters per sea mile
33 #define METERS_PER_SEA_MILE     1852.0
34
35 // 输入参数: MyLatLong_T 是个结构,包括经度和纬度
36 // 输出参数: dDistance 就是返回的大圆距离
37 void CLatLonDistanceDlg::DistanceOfTwoPositions(MyLatLong_T StartLatLong, MyLatLong_T EndLatLong, double &dDistance)
38 {
39     double DInRadians;
40     double dTmpVal = 0.0;
41     double dFi = 0.0;
42     double dFi2 = 0.0;
43     double dDrda = EndLatLong.dLongitude - StartLatLong.dLongitude;
44     double dTmp = 0.0;
45     double dTmp2 = 0.0;
46
47     dDrda = dDrda * gConstPI / 180.0;   // in radians
48     dFi = StartLatLong.dLatitude;
49     dFi2 = EndLatLong.dLatitude;
50
51     dFi = dFi * gConstPI / 180.0;       // in radians
52     dFi2 = dFi2 * gConstPI / 180.0;     // in radians
53
54     dTmpVal = sin(dFi) * sin(dFi2) + cos(dFi) * cos(dFi2) * cos(dDrda);
55     if(fabs(dTmpVal) > 1.0)
56     {
57         AfxMessageBox(L"Invalidate value of arccos!");      // Use Unicode Character Set
58         return ;
59     }
60     DInRadians = acos(dTmpVal);         // in radians
61     dTmp = (sin(dFi) + sin(dFi2));
62     dTmp2 = (sin(dFi) - sin(dFi2));
63     dTmpVal = ((3 * sin(DInRadians) - DInRadians) * dTmp * dTmp ) / (1 + cos(DInRadians));
64     dTmpVal = dTmpVal - ((3 * sin(DInRadians) + DInRadians) * dTmp2 * dTmp2) / (1 - cos(DInRadians));
65
66     dDistance = EARTH_LONG_RADIUS_WGS84 * DInRadians + (EARTH_LONG_RADIUS_WGS84 / (4 * EARTH_FLATTENING_WGS84)) * dTmpVal;//in meters
67 }
68
69 // 测试代码
70 // TODO: 在此添加额外的初始化代码
71 {
72     // 深圳百合酒店 纬度:22.601369,经度114.115145
73     // 深圳百合星城 纬度:22.601334,经度114.115807
74     // 深圳布吉农批 纬度:22.583596,经度114.112227
75     MyLatLong_T LatLon;
76     MyLatLong_T LatLon2;
77     MyLatLong_T LatLon3;
78     double dDis = 0.0;
79
80     LatLon.dLatitude = 22.601369;
81     LatLon.dLongitude = 114.115145;
82     LatLon2.dLatitude = 22.601334;
83     LatLon2.dLongitude = 114.115807;
84     LatLon3.dLatitude = 22.583596;
85     LatLon3.dLongitude = 114.112227;
86
87     DistanceOfTwoPositions(LatLon,LatLon2,dDis);
88     TRACE("%f\r\n",dDis);   // 68.177865
89     DistanceOfTwoPositions(LatLon,LatLon3,dDis);
90     TRACE("%f\r\n",dDis);   // 1990.891295
91 }  
时间: 2024-08-03 08:19:00

两经纬度之间的距离计算的相关文章

转:mysql 下 计算 两点 经纬度 之间的距离 计算结果排序

转自cdsn:https://blog.csdn.net/u013160024/article/details/43635053?utm_source=blogxgwz0 公式如下,单位米: 第一点经纬度:lng1 lat1 第二点经纬度:lng2 lat2 round(6378.138*2*asin(sqrt(pow(sin( (lat1*pi()/180-lat2*pi()/180)/2),2)+cos(lat1*pi()/180)*cos(lat2*pi()/180)* pow(sin(

IOS 计算两个经纬度之间的距离

一 丶 -(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{ CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1]; CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2

计算两个经纬度之间的距离

//计算两个经纬度之间的距离 /** * 计算两点地理坐标之间的距离 * @param Decimal $longitude1 起点经度 * @param Decimal $latitude1 起点纬度 * @param Decimal $longitude2 终点经度 * @param Decimal $latitude2 终点纬度 * @param Int $unit 单位 1:米 2:公里 * @param Int $decimal 精度 保留小数位数 * @return Decimal

sql 计算两经纬度间的距离

DECLARE @g geography;SET @g = geography::STPointFromText('POINT(113.216273 23.236333)', 4326);SELECT @g.ToString();DECLARE @gg geography;SET @gg = geography::STPointFromText('POINT(115.567368 37.341209)', 4326);SELECT @gg.ToString(); SELECT @g.STDist

PHP计算2点经纬度之间的距离代码

以下是对PHP计算2点经纬度之间的距离代码进行了分析介绍,需要的朋友可以过来参考下 <?php function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) { $theta = $longitude1 - $longitude2; $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad

获取两个点经纬度之间的距离

/** * js获取两个经纬度之间的距离 * @param lat1 第一点的纬度 * @param lng1 第一点的经度 * @param lat2 第二点的纬度 * @param lng2 第二点的经度 * @returns {Number} */ function getDistance(lat1, lng1, lat2, lng2) { var radLat1 = lat1*Math.PI / 180.0; var radLat2 = lat2*Math.PI / 180.0; var

计算两个经纬度之间的距离(python算法)

EARTH_REDIUS = 6378.137 def rad(d): return d * pi / 180.0 def getDistance(lat1, lng1, lat2, lng2): radLat1 = rad(lat1) radLat2 = rad(lat2) a = radLat1 - radLat2 b = rad(lng1) - rad(lng2) s = 2 * math.asin(math.sqrt(math.pow(sin(a/2), 2) + cos(radLat1

JAVA实现求一点到另两点连线的距离,计算两点之间的距离

直接上代码 /** *计算两点之间距离 */ public static double getDistance(Point start,Point end){ double lat1=start.getX().doubleValue(); double lat2=end.getX().doubleValue(); double lon1=start.getY().doubleValue(); double lon2=end.getY().doubleValue(); return Math.sq

mysql 下 计算 两点 经纬度 之间的距离 含具体sql语句

文章转载地址 http://blog.sina.com.cn/s/blog_7bbfd5fd01017d1e.html 感谢作者. 在原文的基础上,我新增了sql语句,方便大家理解 mysql距离计算,单位m,以及排序 lon 经度 lat 纬度 一般地图上显示的坐标顺序为,纬度在前(范围-90~90),经度在后(范围-180~180) 首先新建一张表,里面包含经纬度 SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Tab