0

I am creating an app in which I need top 5 nearest riders location from cook location and then store in list an ascending order. I have found the nearest rider location from cook location. But i am bit confused how to add top 5 in list.

That's my code for finding nearest rider location.

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();
        }

1 Answers1

0

I think you just have to replace "smallestDistance" by an array of 5 and then just test every cases and you will have the closest one in the first element of the array and the far one ine the fifth element of the array :

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());
}
Aznhar
  • 610
  • 1
  • 10
  • 30
  • I think this will store the same location. Like top 1st nearest rider. – Muhammad Arsalan Mar 05 '18 at 14:40
  • I don't think, bacause the nearest will never enter in the "else" statement. It will enter one time in the first if and won't be called another time so it won't enter on the other "else" – Aznhar Mar 05 '18 at 14:43
  • Can I use ArrayList rather than array?. Because i have to create dynamic structure. – Muhammad Arsalan Mar 05 '18 at 14:48
  • Yes I think this should work the same way, I haven't tried – Aznhar Mar 05 '18 at 14:50
  • I found a problem when assigning value -1 to array. it set value on index 0. it give me exception ArrayIndexOutOfBoundsException. I think error would be same for ArrayList. @Aznhar – Muhammad Arsalan Mar 05 '18 at 16:00
  • ArrayList are empty by definition, did you add element ? You can't set value without adding element first – Aznhar Mar 05 '18 at 16:57
  • I am currently working on arrays. I created array like: public static Double[] smallestDistance =new Double[]{Double.valueOf(-1)}; – Muhammad Arsalan Mar 05 '18 at 17:13
  • Well in this case, it worked for me : smallestDistance[0] = -1d; – Aznhar Mar 05 '18 at 17:17
  • double, don't forget toadd "d" letter after "-1" so your compiler will understand that -1 is a double – Aznhar Mar 05 '18 at 17:23
  • By using this smallestDistance[0] = -1d you are adding value to index 0. But when i am comparing with index 1 or 2 and so on it the same error. – Muhammad Arsalan Mar 05 '18 at 17:49
  • That's because your array has only one element when you initialized it. You need to make it bigger at the beginning like : Double[] smallestDistance =new Double[]{-1d,-1d,-1d,-1d,-1d}; for 5 value – Aznhar Mar 05 '18 at 17:52
  • What should i do? – Muhammad Arsalan Mar 05 '18 at 17:55
  • For static size of Array : Double[] smallestDistance =new Double[]{-1d,-1d,-1d,-1d,-1d}; And for dynamic : ArrayList smallestDistance = new ArrayList(); smallestDistance.add(-1d); smallestDistance.add(-1d); smallestDistance.add(-1d); smallestDistance.add(-1d); smallestDistance.add(-1d); – Aznhar Mar 05 '18 at 17:57
  • Currently i run it for top 2 riders. It gave me same value like for 1st and 2nd. – Muhammad Arsalan Mar 05 '18 at 18:08