Reputation: 619
In my android app I'm trying to compare user location with a list of stores coordinates.
I already know how to get user coordinates and I already have the list as a array.
How can I compare both and find the nearest store?
Upvotes: 0
Views: 107
Reputation: 22064
float shortestDistance = Float.MAX_VALUE;
Location closestLocation = null;
for(Location loc : locations){
if(yourLocation.distanceTo(loc) < shortestDistance){
shortestDistance = yourLocation.distanceTo(loc);
closestLocation = loc;
}
}
Upvotes: 1