我试图计算一个位置(源位置标记)与存储在ArrayList中的其他位置(从RoutesData返回)之间的距离,并沿另一个ArrayList中的距离存储位置。但是当我在该阵列中添加一个位置和距离源位置的距离时,它将覆盖所有先前存储的位置和距离,并且新添加的位置和距离。我为特定逻辑编写代码。ArrayList覆盖所有以前存储的值android
类RoutesData:返回LatLngs测量距离
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;
}
}
类LocationDistances:结合位置的距离。
public class LocationDistances {
LatLng locs;
double distances;
}
getOverAllRoute()方法在MainActivity:哪个源地点与所有LatLngs比较从的RouteData类返回并以列表srcLocDisList存储它们。我遇到的所有麻烦都在第一循环,它正在计算距离,然后将距离和位置添加到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();
}
你的代码创建一个新的数组。然后发生什么? –
对不起@Alex我编辑了我的代码。 –