1

The following piece of code was instrumental to an iOS app I finished recently:

var closestLocation: CLLocation?
var smallestDistance: CLLocationDistance?

for location in locations {
  let distance = currentLocation.distanceFromLocation(location)
  if smallestDistance == nil || distance < smallestDistance {
    closestLocation = location
    smallestDistance = distance
  }
}

My question is pretty simple: how would this piece of code look in Java for Android Studio?

This is my first run at Java and Android Studio, so any help would be greatly appreciated.

tsteve
  • 549
  • 1
  • 7
  • 16

3 Answers3

4

You should consider using the Location class. It is used to store locations in Latitude and Longitude. It also has predefined function for calculating distance between points - distanceBetween().

Location closestLocation;
int smallestDistance = -1;

Assuming locations is an ArrayList<Location>()

for(Location location : locations){
    int distance = Location.distanceBetween(closestLocation.getLatitude(),
                           closestLocation.getLongitude(),
                           location.getLatitude(),
                           location.getLongitude());
    if(smallestDistance == -1 || distance < smallestDistance){
        closestLocation = location;
        smallestDistance = distance;
    }
}
Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
  • Thanks! I'm using a large set of latitudes and longitudes to construct the ArrayList. How would that be written? – tsteve Mar 04 '16 at 18:15
  • If you are just creating latitude longitude pair then consider making `ArrayList list`. Create new LatLng objects like `LatLng location = new LatLng(latitudeValue, longitudeValue);` and add this to your list like `list.add(location)`. – Jyotman Singh Mar 04 '16 at 18:21
  • I have the following: 'LatLng location1 = new LatLng(35.789882, -78.690761); LatLng location2 = new LatLng(35.789882, -78.690761); ArrayList list = new ArrayList.add(location1).add(location2);' It's throwing an error saying it cannot resole the symbol "add." I'm sure this should be obvious, but I appreciate all your help. It's going a long way. – tsteve Mar 04 '16 at 18:41
  • `ArrayList list = new ArrayList<>();` then in separate statement `list.add(location1);` and `list.add(location2);` – Jyotman Singh Mar 04 '16 at 18:44
  • 1
    Thank you very much! The good people on StackOverflow save the day again. – tsteve Mar 04 '16 at 18:49
2
Location closestLocation = null;
float smallestDistance = -1;

for(Location location:mLocationsList){
float distance  = currentLocation.distanceTo(location);
 if(smallestDistance == -1 || distance < smallestDistance) {
    closestLocation = location
    smallestDistance = distance
  }
}

With mLocationsList being an iterable collection of Location-objects and currentLocation being already set

SimMac
  • 479
  • 5
  • 16
Leonid Veremchuk
  • 1,952
  • 15
  • 27
1

It would look something like this (locations need to be iterable and userLocation must be set):

    Location closest;
    float smallestDistance = Float.MAX_VALUE;
    for (Location l : locations) {
        float dist = l.distanceTo(userLocation);
        if (dist < smallestDistance) {
            closest = l;
            smallestDistance = dist;
        }
    }
siyb
  • 2,837
  • 2
  • 21
  • 18