我正在尝试计算位置(源位置标记)与存储在ArrayList中的其他位置(从RoutesData返回)之间的距离,并沿着距离存储在另一个ArrayList中的位置,然后选择具有最小距离的LatLng。我特定逻辑的代码是。
类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中。我面临的所有麻烦都是在计算最小距离的第二个循环中然后返回与最小距离相关联的对象但是在一次迭代之后应用程序崩溃并且在调试时它给出错误&#34;未找到源。&#34; ?
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
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++){
LocationDistances srcLocDis = new LocationDistances();
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();
}
第二循环内部:
第一次Iterration后:
答案 0 :(得分:1)
我认为你应该移动以下代码行
LocationDistances srcLocDis = new LocationDistances();
进入for循环,如下所示,否则为循环的其余部分覆盖相同的对象
for(int i =0;i<mainRoutePoints.size();i++){
srcLocDis = new LocationDistances();
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());
}