【问题标题】:Calculate distance between two coordinates out of 2 lists计算 2 个列表中的两个坐标之间的距离
【发布时间】: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


【解决方案1】:

正如 cmets 中所说,我同意您根本不应该分开 lat&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);

    [....]
}

但同样,请不要。

【讨论】:

  • 很好的答案,OP,请注意,一旦你上课,你就可以把行为挂在课堂上。例如public double calculateDistanceTo(Position other)。然后你开始利用 OO 的好处。
【解决方案2】:
        //  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;
        }

【讨论】:

  • 如果您对此答案有任何疑问,请咨询我
  • 嘿,谢谢您的回答,但我对 java 很陌生,如何设置 getter 和 setter 以及 create 是什么意思?
  • getter 和 setter 是设置变量值的简单函数,我将添加函数
  • 现在他在 cordinateList.add(new Cordinate(route.getLattitude,rout.getLongitude));
  • 当您调用函数 route.getLatitude() 时返回哪种类型的数据?
猜你喜欢
  • 2010-09-26
  • 2020-02-12
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
  • 2016-07-16
  • 1970-01-01
相关资源
最近更新 更多