这个算是自己保存的一个小demo吧,方便以后使用,不用再度娘上到处乱找。
先从方法上接收经纬度的参数:latitude和longitude,然后直接套用下面计算。s就是距离,单位是(米),若要单位修改为公里,将s*1000修改为s*10000即可
Map map2 = new HashMap();
//将传入进来的经纬度参数,转换为Double类型,方便计算。
Double double_latitude = new Double(latitude);
Double double_longitude = new Double(longitude);
double radLat1 = double_latitude * Math.PI / 180.0;
double radLat2 = (store_list.get(i).getStore_lat()).doubleValue()* Math.PI / 180.0;
double a = radLat1 - radLat2;
double b = double_longitude* Math.PI / 180.0 - (store_list.get(i).getStore_lng()).doubleValue()* Math.PI / 180.0;
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 * r;
s = Math.round(s * 1000);
map2.put("store_farAway", s);