我在Android中使用谷歌地方api但我也想找到我当前位置到地图上显示的特定位置之间的距离请帮我找到。因为当我从地方api获取位置值后使用方向api时需要花费太多时间而不能轻易显示结果请帮帮我
答案 0 :(得分:1)
尝试这种方法:
public double CalculationsByDistance(){
// earth radius
int Radius = 6371;
// set your original and destination latitude longitude
double lat1 = new_lat;
double lat2 = dest_lat;
double lon1 = new_long;
double lon2 = dest_long;
double d_lat = Math.toRadians(lat2 - lat1);
double d_lon = Math.toRadians(lon2 - lon1);
double a = Math.sin(d_lat / 2) * Math.sin(d_lat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(d_lon / 2) * Math.sin(d_lon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
hasil = Radius * c;
double kilo_meter = hasil / 1;
DecimalFormat formatBaru = new DecimalFormat("####");
int kilometer_dlm_desimal = Integer.valueOf(formatBaru.format(kilo_meter));
e = new Integer(kilometer_dlm_desimal).toString();
double meter = hasil * 1000;
int meter_dlm_decimal = Integer.valueOf(formatBaru.format(meter));
d = new Integer(meter_dlm_decimal).toString();
Log.i("Radius ", "" + hasil + " KM " + kilometer_dlm_desimal + " Meter " + meter_dlm_decimal);
return Radius * c;
}
答案 1 :(得分:0)
使用此方法计算两个位置之间的距离。 这将返回以米为单位的近似距离。
请将源位置和目标位置传递给此方法。
private double calculateDistance(Location source, Location destination){
double distance = 0;
try {
distance = source.distanceTo(destination);
return distance;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}