我试图用纬度和经度找到两个坐标之间的距离。我使用以下方法:
private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
我从前一个活动的捆绑中获取当前的纬度和经度,我按如下方式调用该方法:
String parameter=""+distance(current_latitude,current_longitude,Double.parseDouble(store_list.get(position).store_latitude),Double.parseDouble(store_list.get(position).store_longitude),'K');
但是我得到了像8500等荒谬的结果值。请帮助
答案 0 :(得分:1)
考虑使用Location
类提供的方法:http://developer.android.com/reference/android/location/Location.html#distanceBetween(double, double, double, double, float[])
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Parameters
startLatitude the starting latitude
startLongitude the starting longitude
endLatitude the ending latitude
endLongitude the ending longitude
results an array of floats to hold the results
用法:
float[] results = new float[3];
Location.distanceBetween(current_latitude, current_longitude, Double.parseDouble(store_list.get(position).store_latitude), Double.parseDouble(store_list.get(position).store_longitude, results);
float distanceMeters = results[0];