簡體   English   中英

計算2個列表中兩個坐標之間的距離

[英]Calculate distance between two coordinates out of 2 lists

嗨,我有一個問題。

我在我的應用程序中實現了Google Maps,並將坐標保存到SQL Lite數據庫中(我也有一個“開始”和“停止”按鈕)。 現在,我要計算起點和終點之間的距離。 我從兩個列表中獲取坐標(一個用於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);

    }

正如評論中所說,我同意您完全不應將經緯分開。 您可以為它們都創建一個存儲類:

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);

    [....]
}

但同樣,請不要。

        //  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;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM