Sourabh Saldi
Sourabh Saldi

Reputation: 3587

Google map v2 find if current location lies in circle

How to find if current location lies inside a circle on google map v2

Upvotes: 0

Views: 775

Answers (2)

zbr
zbr

Reputation: 7037

You need to have three things:

  1. A Location object with the location of the circle's center.
  2. A Location object with the current location.
  3. The circle's radius.

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

Luser_k
Luser_k

Reputation: 534

Use this, if you want to use geofencing functionality, if thats is your aim in your app.

Geofencing in android

Upvotes: 1

Related Questions