[英]Getting absurd values when trying to calculate distance between two coordinates in Android
I am trying to find distance between two coordinates using latitudes and longitudes.I am using the following method: 我正在尝试使用经度和纬度来查找两个坐标之间的距离。我正在使用以下方法:
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);
}
I am getting the current latitude and longitude from bundle from a previous activity and i am calling the method as follows: 我从上一个活动的捆绑中获取当前的经度和纬度,并且按如下方式调用该方法:
String parameter=""+distance(current_latitude,current_longitude,Double.parseDouble(store_list.get(position).store_latitude),Double.parseDouble(store_list.get(position).store_longitude),'K');
But i am getting absurd results values like 8500 etc.Please help 但是我得到像8500等的荒谬结果值。请帮助
Consider using the method provided by the Location
class: http://developer.android.com/reference/android/location/Location.html#distanceBetween(double, double, double, double, float[]) 考虑使用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
usage: 用法:
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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.