15

I'm trying to know if a given marker is inside a circle radius.

In javascript, i can do something like:

google.maps.geometry.spherical.computeDistanceBetween(latLngCircleCenter, latLngPoint);

But in android, using maps-v2, i'm stucked.

enter image description here

Otuyh
  • 2,384
  • 5
  • 22
  • 40

3 Answers3

44

After a lot of research, i found a very simple solution:

float[] distance = new float[2];

Location.distanceBetween( marker.getPosition().latitude, marker.getPosition().longitude,
    circle.getCenter().latitude, circle.getCenter().longitude, distance);

if( distance[0] > circle.getRadius()  ){
    Toast.makeText(getBaseContext(), "Outside", Toast.LENGTH_LONG).show();
} else {
    Toast.makeText(getBaseContext(), "Inside", Toast.LENGTH_LONG).show();
}

I'm testing this and it's working. Can someone test too and give the feedback here?

Nicktar
  • 5,548
  • 1
  • 28
  • 43
Otuyh
  • 2,384
  • 5
  • 22
  • 40
  • @Otuyh Please insert your complete answer. –  Jul 01 '14 at 11:09
  • 1
    I don't think this solution will be very accurate for large radii since it doesn't take into account the curvature of the earth. You should use the Haversine formula if accuracy is of importance. – lentz Jun 22 '16 at 01:43
1

The same APIs you'd use in Javascript are available for Android here: https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/SphericalUtil.java

Chris Broadfoot
  • 5,082
  • 1
  • 29
  • 37
FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
0

I think its possible to find the Centre of the circle, you are defining the circle in your code with repect to a given geopoint. You can save it and use later. Then try Location.distanceBetween(..)

Jashan PJ
  • 4,177
  • 4
  • 27
  • 41