java计算距离_java实现计算地理坐标之间的距离
java實現計算兩經緯度點之間的距離,直接上代碼,具體解釋請參考注釋
package com.jttx.poi.utils;
import com.jttx.poi.entity.point;
/**
* created by louis on 2014/9/2.
*/
public class geoutils {
/**
* 計算兩經緯度點之間的距離(單位:米)
* @param lng1? 經度
* @param lat1? 緯度
* @param lng2
* @param lat2
* @return
*/
public static double getdistance(double lng1,double lat1,double lng2,double lat2){
double radlat1 = math.toradians(lat1);
double radlat2 = math.toradians(lat2);
double a = radlat1 - radlat2;
double b = math.toradians(lng1) - math.toradians(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 * 6378137.0;// 取wgs84標準參考橢球中的地球長半徑(單位:m)
s = math.round(s * 10000) / 10000;
return s;
}
/**
* 計算tp值
* @param curpoint????? 當前點
* @param relatedpoint? 偏移點
* @param isgeography?? 是否是地理坐標 false為2d坐標
* @return????????????? tp值
*/
public static double getdirangle(point curpoint,point relatedpoint,boolean isgeography){
double result = 0;
if(isgeography){
double y2 = math.toradians(relatedpoint.getlat());
double y1 = math.toradians(curpoint.getlat());
double alpha = math.atan2(relatedpoint.getlat() - curpoint.getlat(), (relatedpoint.getlng() - curpoint.getlng()) * math.cos((y2 - y1) / 2));//緯度方向乘以cos(y2-y1/2)
double delta =alpha<0?(2*math.pi+alpha):alpha;
result = math.todegrees(delta);
}else {
double alpha = math.atan2(relatedpoint.getlat() - curpoint.getlat(), relatedpoint.getlng() - curpoint.getlng());
double delta=alpha<0?(2*math.pi+alpha):alpha;
result = math.todegrees(delta);
}
return result;
}
public static void main(string[] args) {
system.out.println(getdistance(121.446014,31.215937,121.446028464238,31.2158502442799? ));
}
}
以上就是本文的全部內容了,希望大家能夠喜歡。
希望與廣大網友互動??
點此進行留言吧!
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java计算距离_java实现计算地理坐标之间的距离的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: long在C语言中是非法字符吗,C程序设
- 下一篇: bloomberg用法 固定收益_干货