Reputation: 3587
How to find if current location lies inside a circle on google map v2
Upvotes: 0
Views: 775
Reputation: 7037
You need to have three things:
Location
object with the location of the circle's center.Location
object with the current location.Then you can use the method Location.distanceTo(Location)
and compare the result with the radius.
Example:
So I assume you have a reference to a Circle
in your map:
Circle circle;
Then, I guess your activity implements LocationListener
and you use the method onLocationChanged(Location)
to get notified everytime the location changes. In that method, you can do something like this:
@Override
public void onLocationChanged(Location location) {
Location l = new Location("");
l.setLatitude(circle.getCenter().latitude);
l.setLongitude(circle.getCenter().longitude);
if (location.distanceTo(l) > circle.getRadius()) {
// outside of circle
} else {
// inside
}
}
(Untested code.)
Something like this.
Upvotes: 0
Reputation: 534
Use this, if you want to use geofencing functionality, if thats is your aim in your app.
Upvotes: 1