我正在创建一个应用程序,其中我需要距离厨师位置最近的5个最近的车手位置,然后按列表升序存储。我从厨师的位置找到了最近的骑手位置。但我有点困惑如何在列表中添加前5名。
这是我找到最近骑手位置的代码。
try {
for(Location rlocation : rLocations){
float distance= cookerLOCATION.distanceTo(rlocation);
//int distance = Location.distanceBetween(cookLAT,cookLONG,rlocation.getLatitude(),rlocation.getLongitude(),results);
if(smallestDistance == -1 || distance < smallestDistance){
colsestRiderLocation = rlocation;
smallestDistance = distance;
comparingValues();
Log.d("Closet Rider Location",colsestRiderLocation.toString());
}
}
}catch (Exception e){
e.printStackTrace();
}
答案 0 :(得分:0)
我认为你只需要用5的数组替换“smallestDistance”,然后只测试每个情况,你将在数组的第一个元素中得到最接近的一个,而在数组的第五个元素中得到最远的一个:
if(smallestDistance[0] == -1 || distance < smallestDistance[0]){
colsestRiderLocation = rlocation;
smallestDistance[0] = distance;
comparingValues();
Log.d("Closet Rider Location",colsestRiderLocation.toString());
}
else if(smallestDistance[1] == -1 || distance < smallestDistance[1]){
colsestRiderLocation = rlocation;
smallestDistance[1] = distance;
Log.d("Second closet Rider Location",colsestRiderLocation.toString());
}
else if(smallestDistance[2] == -1 || distance < smallestDistance[2]){
colsestRiderLocation = rlocation;
smallestDistance[2] = distance;
Log.d("Third closet Rider Location",colsestRiderLocation.toString());
}
else if(smallestDistance[3] == -1 || distance < smallestDistance[3]){
colsestRiderLocation = rlocation;
smallestDistance[3] = distance;
Log.d("Fourth closet Rider Location",colsestRiderLocation.toString());
}
else if(smallestDistance[4] == -1 || distance < smallestDistance[4]){
colsestRiderLocation = rlocation;
smallestDistance[4] = distance;
Log.d("Fifth closet Rider Location",colsestRiderLocation.toString());
}