给出两点经纬度算距离

 1 <span style="font-size:14px;font-weight: normal;"> 
 2 private static final  double EARTH_RADIUS = 6378137;//赤道半径(单位m)  
 3       
 4     /** 
 5      * 转化为弧度(rad) 
 6      * */  
 7     private static double rad(double d)  
 8     {  
 9        return d * Math.PI / 180.0;  
10     }  
11       
12     /** 
13      * 基于余弦定理求两经纬度距离 
14      * @param lon1 第一点的精度 
15      * @param lat1 第一点的纬度 
16      * @param lon2 第二点的精度 
17      * @param lat3 第二点的纬度 
18      * @return 返回的距离,单位km 
19      * */  
20     public static double LantitudeLongitudeDist(double lon1, double lat1,double lon2, double lat2) {  
21         double radLat1 = rad(lat1);  
22         double radLat2 = rad(lat2);  
23   
24         double radLon1 = rad(lon1);  
25         double radLon2 = rad(lon2);  
26   
27         if (radLat1 < 0)  
28             radLat1 = Math.PI / 2 + Math.abs(radLat1);// south  
29         if (radLat1 > 0)  
30             radLat1 = Math.PI / 2 - Math.abs(radLat1);// north  
31         if (radLon1 < 0)  
32             radLon1 = Math.PI * 2 - Math.abs(radLon1);// west  
33         if (radLat2 < 0)  
34             radLat2 = Math.PI / 2 + Math.abs(radLat2);// south  
35         if (radLat2 > 0)  
36             radLat2 = Math.PI / 2 - Math.abs(radLat2);// north  
37         if (radLon2 < 0)  
38             radLon2 = Math.PI * 2 - Math.abs(radLon2);// west  
39         double x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1);  
40         double y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1);  
41         double z1 = EARTH_RADIUS * Math.cos(radLat1);  
42   
43         double x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2);  
44         double y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2);  
45         double z2 = EARTH_RADIUS * Math.cos(radLat2);  
46   
47         double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)+ (z1 - z2) * (z1 - z2));  
48         //余弦定理求夹角  
49         double theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS));  
50         double dist = theta * EARTH_RADIUS;  
51         return dist;  
52 }
53 </span> 

 

转载于:https://www.cnblogs.com/qiangshu/p/4596705.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值