當前位置:首頁 » 操作系統 » api演算法

api演算法

發布時間: 2023-03-03 05:58:15

Ⅰ 如何利用百度地圖api計算兩點間的實際距離

public String getLatLngDistance(LatLng start, LatLng end){
//自己實現距離演算法
/**
* 計算兩點之間距離
* @param start
* @param end
* @return String 多少m , 多少km
*/

double lat1 = (Math.PI/180)*start.latitude;
double lat2 = (Math.PI/180)*end.latitude;

double lon1 = (Math.PI/180)*start.longitude;
double lon2 = (Math.PI/180)*end.longitude;

// double Lat1r = (Math.PI/180)*(gp1.getLatitudeE6()/1E6);
// double Lat2r = (Math.PI/180)*(gp2.getLatitudeE6()/1E6);
// double Lon1r = (Math.PI/180)*(gp1.getLongitudeE6()/1E6);
// double Lon2r = (Math.PI/180)*(gp2.getLongitudeE6()/1E6);

//地球半徑
double R = 6371.004;

//兩點間距離 m,如果想要米的話,結果*1000就可以了
double dis = Math.acos(Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1))*R;
NumberFormat nFormat = NumberFormat.getNumberInstance(); //數字格式化對象
if(dis < 1){ //當小於1千米的時候用,用米做單位保留一位小數

nFormat.setMaximumFractionDigits(1); //已可以設置為0,這樣跟網路地圖APP中計算的一樣
dis *= 1000;

return nFormat.format(dis)+"m";
}else{
nFormat.setMaximumFractionDigits(2);
return nFormat.format(dis)+"km";
}

}

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:594
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:889
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:583
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:766
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:685
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1014
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:257
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:116
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:808
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:714