From 0ec77b31f84e72b68bd8f11a122b6502051b210f Mon Sep 17 00:00:00 2001 From: misaochan Date: Wed, 28 Mar 2018 20:45:08 +1000 Subject: [PATCH] Add check for size of places array --- .../nrw/commons/nearby/NearbyController.java | 70 ++++++++++--------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java index 0ecf091601..015d221353 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java @@ -54,41 +54,47 @@ public NearbyPlacesInfo loadAttractionsFromLocation(LatLng curLatLng) { } List places = nearbyPlaces.getFromWikidataQuery(curLatLng, Locale.getDefault().getLanguage()); - LatLng[] boundaryCoordinates = {places.get(0).location, // south - places.get(0).location, // north - places.get(0).location, // west - places.get(0).location};// east, init with a random location - - if (curLatLng != null) { - Timber.d("Sorting places by distance..."); - final Map distances = new HashMap<>(); - for (Place place: places) { - distances.put(place, computeDistanceBetween(place.location, curLatLng)); - // Find boundaries with basic find max approach - if (place.location.getLatitude() < boundaryCoordinates[0].getLatitude()) { - boundaryCoordinates[0] = place.location; - } - if (place.location.getLatitude() > boundaryCoordinates[1].getLatitude()) { - boundaryCoordinates[1] = place.location; - } - if (place.location.getLongitude() < boundaryCoordinates[2].getLongitude()) { - boundaryCoordinates[2] = place.location; - } - if (place.location.getLongitude() > boundaryCoordinates[3].getLongitude()) { - boundaryCoordinates[3] = place.location; + if (places.size() > 0) { + LatLng[] boundaryCoordinates = {places.get(0).location, // south + places.get(0).location, // north + places.get(0).location, // west + places.get(0).location};// east, init with a random location + + + if (curLatLng != null) { + Timber.d("Sorting places by distance..."); + final Map distances = new HashMap<>(); + for (Place place : places) { + distances.put(place, computeDistanceBetween(place.location, curLatLng)); + // Find boundaries with basic find max approach + if (place.location.getLatitude() < boundaryCoordinates[0].getLatitude()) { + boundaryCoordinates[0] = place.location; + } + if (place.location.getLatitude() > boundaryCoordinates[1].getLatitude()) { + boundaryCoordinates[1] = place.location; + } + if (place.location.getLongitude() < boundaryCoordinates[2].getLongitude()) { + boundaryCoordinates[2] = place.location; + } + if (place.location.getLongitude() > boundaryCoordinates[3].getLongitude()) { + boundaryCoordinates[3] = place.location; + } } + Collections.sort(places, + (lhs, rhs) -> { + double lhsDistance = distances.get(lhs); + double rhsDistance = distances.get(rhs); + return (int) (lhsDistance - rhsDistance); + } + ); } - Collections.sort(places, - (lhs, rhs) -> { - double lhsDistance = distances.get(lhs); - double rhsDistance = distances.get(rhs); - return (int) (lhsDistance - rhsDistance); - } - ); + nearbyPlacesInfo.placeList = places; + nearbyPlacesInfo.boundaryCoordinates = boundaryCoordinates; + return nearbyPlacesInfo; + } + else { + return null; } - nearbyPlacesInfo.placeList = places; - nearbyPlacesInfo.boundaryCoordinates = boundaryCoordinates; - return nearbyPlacesInfo; } /**