C语言根据经纬度计算距离[通俗易懂]

2022-11-09 14:13:43 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

#include <math.h>

#define EARTH_RADIUS 6378.137//地球半径 #define PI 3.14159265358979323846 //圆周率

//角度转化为弧度 static double rad(double d) { return d * PI / 180.0; }

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 * asin(sqrt(pow(sin(a/2),2) cos(radLat1)*cos(radLat2)*pow(sin(b/2),2))); s = s * EARTH_RADIUS; //s = round(s * 10000) / 10000; s = llroundl(s*10000) / 10000.0; //s = double b = ( ( int )( a * 1000000 0.5 ) ) / 1000000.0;//round(11111111.21); //s = s/10000; return s; }

int main()

{

double dist = GetDistance(0,1, 0, 1.001 ); printf(“1.dist=%fn”, dist ); dist = GetDistance(90-24.0,0, 90 24.01, 0 ); printf(“2.dist=%fn”, dist ); dist = GetDistance(24.0,5, -24, -1 ); printf(“3.dist=%fn”, dist ); dist = GetDistance(24,5, 24, -1 ); printf(“4.dist=%fn”, dist ); dist = GetDistance(48,5, 24, -1 ); printf(“5.dist=%fn”, dist ); dist = GetDistance(-50.0,120, -10, 120 ); printf(“6.dist=%fn”, dist );

while(1);

return 0;

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/190383.html原文链接:https://javaforall.cn

0 人点赞