注:Location类为自定义的实体类,里面包含latitude和longitude两个属性(Double类型)
/**
* 计算两点之间距离
* @param start
* @param end
* @return 米
*/
public Double getDistance(Location start,Location end){
double lat1 = (Math.PI/180)*start.getLatitude();
double lat2 = (Math.PI/180)*end.getLatitude();
double lon1 = (Math.PI/180)*start.getLongitude();
double lon2 = (Math.PI/180)*end.getLongitude();
//地球半径
double R = 6378.137;
//两点间距离 km,如果想要米的话,结果*1000
double d = Math.acos(Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1))*R;
if(d<1)
return (Double)(d*1000);
else
return Double.valueOf(String.format("%.2f",d*1000));
}