Nick Otten
Nick Otten

Reputation: 712

comparing GPS cordinates with a certain margin

I'm currently working on a app where I want to check if the mobile phone is near a certain location. This location is loaded out of a list of objects that have a location attached to them. Locations are stored as longitude and latitude double value. The current location (witch is received from the GPS adapter) are temporally stored in doubles as well.

Now I know I can just use a if statement to check if latitude==current_latitude. But I want to apply a certain margin to it. I want to check if i'm in a area that is (for example) withing 1000 meter of the stored location. I try'd some googling but had no success as I'm not really sure what to look for. If anyone could point me in the right direction that would be hugely appreciated.

Thanks in advance!

Upvotes: 0

Views: 118

Answers (1)

AlexWien
AlexWien

Reputation: 28727

You are near the location if you are inside a circle around the location.
A point is inside a circle if the distance from the point to the center of the circle is smaller than the circles radius:

For example set radius = 1000m. Use the distance calculation of the Location class: Then

// radius in meter
double radius = 1000;

Location curLoc;
Location destination;
       ...
if (curLoc.distanceTo(destination) < radius)) {
isInside = true;
}

Upvotes: 1

Related Questions