【发布时间】:2014-02-19 15:46:07
【问题描述】:
大家好,我有一个问题。
我在我的应用程序中实现了 Google 地图,并将获得的坐标保存到 SQL Lite 数据库中(我还有一个开始和停止按钮)。现在我想计算起点和终点之间的距离。我从两个列表中获取坐标(一个用于纬度,一个用于经度),我现在的问题是如何遍历列表并从中获取坐标。我还需要一次读出 2 个坐标,因为我需要开始和结束坐标,我需要这样做直到列表完成。
所以我希望有人知道这个问题的解决方案,这是我的代码:
public double calculateKM() {
allRoute = (ArrayList<Route>) db.getAllRouteByID(drive_id);
for (Route route : allRoute) {
Log.d("Car: ", route.getDate());
lat.add(route.getLatitude());//here I get the coordiantes out of the database
lon.add(route.getLongitude());
for (int i = 0; i < lat.size(); i++) {//and this is where I cant find a solution.
String element = lat.get(i);
double value = Double.parseDouble(element);
}
calculateDistance(...);
}
}
private double calculateDistance(double fromLong, double fromLat,
double toLong, double toLat) {
double d2r = Math.PI / 180;
double dLong = (toLong - fromLong) * d2r;
double dLat = (toLat - fromLat) * d2r;
double a = Math.pow(Math.sin(dLat / 2.0), 2) + Math.cos(fromLat * d2r)
* Math.cos(toLat * d2r) * Math.pow(Math.sin(dLong / 2.0), 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = 6367000 * c;
return Math.round(d);
}
【问题讨论】:
-
Hi frnd...您使用两个列表视图来保存纬度和经度...您可以为此使用一个 arrayList
-
您不应该使用两个列表来表示纬度和经度。这两个值决定了你的确切位置,不要分开它们,你不会有这个问题。
-
没问题...但是很费时间..
标签: java android list coordinates