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