0

我正在创建一个应用程序,其中我需要距离厨师位置最近的 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();
        }
4

1 回答 1

0

我认为您只需将“smallestDistance”替换为 5 的数组,然后只需测试每个案例,您将在数组的第一个元素中拥有最接近的一个,而在数组的第五个元素中拥有最远的一个:

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());
}
于 2018-03-05T14:16:20.797 回答