Michel
Michel

Reputation: 619

compare my GPS location to a list - android

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

Answers (1)

Carnal
Carnal

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

Related Questions