我正在尝试从我的位置(从谷歌地图)获取附近事件的距离并将其显示在活动上。但我不确定我需要使用哪个按钮来显示距离以及用于显示活动距离(公里)的java代码是什么?
答案 0 :(得分:0)
如果您知道当前的LatLong或目标位置lat很长,那么您可以使用以下方法找到距离。
public double findDistance(LatLng fromLatLan, LatLng toLatLan) {
int earthRadious = 6371;// radius of earth in Km
double latFrom = fromLatLan.latitude;
double latTo = toLatLan.latitude;
double lonFrom = fromLatLan.longitude;
double lonTo = toLatLan.longitude;
double diffLat = Math.toRadians(latTo - latFrom);
double diffLon = Math.toRadians(lonTo - lonFrom);
double aValue = Math.sin(diffLat / 2) * Math.sin(diffLat / 2)
+ Math.cos(Math.toRadians(latFrom))
* Math.cos(Math.toRadians(latTo)) * Math.sin(diffLon / 2)
* Math.sin(diffLon / 2);
double c = 2 * Math.asin(Math.sqrt(aValue));
double results = earthRadious * c;
double kms = results / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int killoMeter = Integer.valueOf(newFormat.format(kms));
double meter = results % 1000;
int metersDistance = Integer.valueOf(newFormat.format(meter));
Log.i("Value", "" + results + " KilloMeter " + killoMeter
+ " Meter " + metersDistance);
return earthRadious * c;
}