计算2个列表中两个坐标之间的距离

时间:2014-01-28 11:10:48

标签: java android list coordinates

嘿伙计们我有一个问题。

我在我的应用程序中实现了Google Maps并将我获得的坐标保存到SQL Lite数据库中(我还有一个Start和Stop按钮)。现在我想计算起点和终点之间的距离。我从两个列表中获取坐标(一个用于Latidude,一个用于经度),我现在的问题是如何迭代列表并从中获取坐标。我也需要同时读出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);

    }

2 个答案:

答案 0 :(得分:1)

正如评论中所述,我同意你不应该将lat&amp; lon分开。您可以为它们创建存储类:

class Position{
    public final double lat;
    public final double lon;

    public Position(double lat, double lon){
        this.lat = lat;
        this.lon = lon;
    }
}

并将其存储在列表中:

List<Position> positions = new ArrayList<Position>();
positions.add( new Position( lat, lon) );

迭代它

for(int i=0; i<positions.size()-1; i++){
   Position a = positions.get(i);
   Position b = positions.get(i+1);

   [.....]
}

如果你想坚持使用双重列表,你可以轻松地遍历它们:

for(int i=0; i<lat.size(); i++){
    String latStr = lat.get(i);
    String lonStr = lon.get(i);

    [....]
}

但是,请不要。

答案 1 :(得分:0)

        //  create a class for saving latitude and longitude under any package
        private class Cordinate
        {
        double lati,longi;

        public Cordinate(double la,louble lo)
        {
           lati=la;
           longi=lo;
        }
        // set getters and setters for this two variables
        public double getLati() {
    return lati;
}

public void setLati(double lati) {
    this.lati = lati;
}

public double getLongi() {
    return longi;
}

public void setLongi(double longi) {
    this.longi = longi;
}



        }
        //define array list for saving the latitude and longitude
        List<Cordinate> cordinateList = new ArrayList<Cordinate>();


        // then inside your functn
        for (Route route : allRoute) {
                Log.d("Car: ", route.getDate());

                cordinateList.add(new Cordinate(route.getLattitude,rout.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);
                }
    // then call the method CalculateDistance () by passing variables
    // You can use cordinate variables for setting the location object
    //  for ex: loc.setLatitude(cordinateList(i).getLati());
    //          loc.setLatitude(cordinateList(i).getLongi()); 


        // method for find distance
        CalculateDistance(Location loc1,Location dest)
        {
          double distance=DistanceCalculator.getDistance(loc, dest);
                        distance=Math.round(distance * 100.0) / 100.0;
        }