0
我試圖計算一個位置(源位置標記)與存儲在ArrayList中的其他位置之間的距離(從RoutesData返回),並將距離存儲在另一個ArrayList中然後選擇距離最短的LatLng。我的代碼是特定的邏輯。從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存儲它們。我面臨的所有麻煩都在第二個循環中,計算最小距離,然後返回與最小距離相關的對象,但是在迭代之後,應用程序崩潰並且在調試時給出錯誤「未找到源」。
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後:
其餘能否請您發佈您的代碼的第二個環和logcat的消息 – dora
@dora我editted問題。沿着日誌文件 –
發佈logcat請 –