ArrayList over写入所有先前存储的值android

时间:2015-04-19 07:27:32

标签: android google-maps arraylist

我正在尝试计算位置(源位置标记)与存储在ArrayList中的其他位置(从RoutesData返回)之间的距离,并将位置沿着距离存储在另一个ArrayList中。但是当我在该数组中添加一个位置及其与源loacation的距离时,它会覆盖所有先前存储的位置和距离以及新添加的位置和距离。我的特定逻辑的代码是。

类RoutesData:返回LatLng以测量距离

public class RoutesData {



    public ArrayList<LatLng> allRoutesMainStops(){
        ArrayList<LatLng> allPoints = new ArrayList<LatLng>(Arrays.asList(
                //icPoints
                new LatLng(33.582752, 73.044503),new LatLng(33.595504, 73.050912),
                //station
                new LatLng(33.601097, 73.047798),new LatLng(33.598755, 73.055607),new LatLng(33.599970, 73.063225),new LatLng(33.602757, 73.066996),new LatLng(33.604297, 73.075843),new LatLng(33.608692, 73.082024),
                //ali nawaz
                new LatLng(33.617330, 73.081743),
                //centre hosp
                new LatLng(33.630072, 73.071996),new LatLng(33.631454, 73.072416),new LatLng(33.633905, 73.062379),new LatLng(33.641462, 73.063376),new LatLng(33.646714, 73.064095),new LatLng(33.651782, 73.064535),new LatLng(33.661329, 73.063974),new LatLng(33.672490, 73.055906),new LatLng(33.683642, 73.047215),new LatLng(33.689706, 73.030363),new LatLng(33.680982, 73.018654),new LatLng(33.671293, 73.016894),
                //gpo 1
                new LatLng(33.595215, 73.051496),new LatLng(33.593119, 73.054184),new LatLng(33.585280, 73.066763),new LatLng(33.588925, 73.076172),new LatLng(33.599117, 73.080001),new LatLng(33.607101, 73.083641),new LatLng(33.626583, 73.075027),
                //new LatLng(33.630072, 73.071996), //duplicate center
                new LatLng(33.631550, 73.072534),new LatLng(33.639063, 73.075742),new LatLng(33.643424, 73.077372),new LatLng(33.650480, 73.080152),new LatLng(33.663188, 73.085446),new LatLng(33.696970, 73.062966),new LatLng(33.699527, 73.073920),new LatLng(33.704257, 73.082993),new LatLng(33.707380, 73.088906),new LatLng(33.717143, 73.082961), new LatLng(33.718617, 73.084589), new LatLng(33.720660, 73.083891), new LatLng(33.727328, 73.073823), new LatLng(33.720397, 73.058392), new LatLng(33.733174, 73.087104)

                ));
        return allPoints;
    }

}

类位置距离:将位置与距离绑定。

public class LocationDistances {
    LatLng locs;
    double distances;
}
MainActivity中的

getOverAllRoute()方法:,它将源位置与从RouteData类返回的所有LatLng进行比较,并将它们存储在列表srcLocDisList中。我面临的所有麻烦都在 First循环中,它计算距离,然后将距离和位置添加到srcDistList,但是当添加新对象时,它会用新添加的对象覆盖所有先前的对象。

public void getOverAllRoute(){
        //to get main route points to measure distance from
        RoutesData rD = new RoutesData();
        ArrayList<LatLng> mainRoutePoints = rD.allRoutesMainStops();
        //Toast.makeText(this,"Size: "+mainRoutePoints.size(), Toast.LENGTH_LONG).show();
        //to store location + distances from source
        LocationDistances srcLocDis = new LocationDistances(); 
        ArrayList<LocationDistances> srcLocDisList = new ArrayList();
        //showMarkerslongLat();
        //create source Location
        Location srcLoc = new Location("");
        srcLoc.setLatitude(sll.latitude);
        srcLoc.setLongitude(sll.longitude);

        // to compare distances from source location
        Location mainPointsLoc = new Location("");
        for(int i =0;i<mainRoutePoints.size();i++){

            mainPointsLoc.setLatitude(mainRoutePoints.get(i).latitude);
            mainPointsLoc.setLongitude(mainRoutePoints.get(i).longitude);
            //store distances and location in arraylist
            srcLocDis.locs = mainRoutePoints.get(i);
            srcLocDis.distances = srcLoc.distanceTo(mainPointsLoc);



            srcLocDisList.add(srcLocDis);
            Log.d("Location data: ",srcLocDis.locs.toString());
            Log.d("Saved Data: ",srcLocDisList.get(i).toString());

        }
        //Toast.makeText(this,"items1: "+srcLocDisList.size(), Toast.LENGTH_LONG).show();
        LocationDistances min=null;
        for(LocationDistances x:srcLocDisList){
            String srcLocDisLocFile = x.locs.latitude+" "+x.locs.longitude+" distances:"+x.distances;


            min=(min==null||x.distances<min.distances)?x:min;
            Log.d("LocDist:",Double.toString(x.distances));

        }
        LatLng srcStartMin=min.locs;
        Toast.makeText(this,srcStartMin.latitude+" "+srcStartMin.longitude+" Distance"+Double.toString(min.distances), Toast.LENGTH_LONG).show();

    }

1 个答案:

答案 0 :(得分:4)

LocationDistances srcLocDis = new LocationDistances();放入循环中,否则您将始终更改同一对象。

for(int i =0;i<mainRoutePoints.size();i++){ 
  LocationDistances srcLocDis = new LocationDistances();
   ....
}