当前位置:首页 » 操作系统 » 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 浏览:115
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:807
python股票数据获取 发布:2025-10-20 07:39:44 浏览:714