0

I am making a map application and I want the user to place a marker on map, select radius from a dialog fragment. How can I check whether user is present within that circular zone. I want to take radius input from user and then check.

Noor
  • 1
  • 1
  • Welcome! :-) what did you try? Could you please add relevant code? – moffeltje Jul 27 '15 at 18:35
  • Take a look at this: http://stackoverflow.com/questions/30431230/do-something-when-im-inside-radius-of-circle And this: http://stackoverflow.com/questions/30170271/android-google-map-how-to-check-if-the-gps-location-is-inside-the-circle/30171471#30171471 – Daniel Nugent Jul 27 '15 at 19:11

2 Answers2

0

You can calculate the distance between the user point and the center.. if it's smaller than the radius, the user is within the radius.

A - Point, B - Center, R - Radius

if distance(A,B) <= R then user is within radius.

Probably the Android SDK has built-in methods to do that.

Rodrigo Reis
  • 1,891
  • 1
  • 11
  • 12
0
 float[] Check_distance = new float[2];//variable to take distance from our location to center of crcle
        String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

        Location.distanceBetween(starting point latitude, starting point longitude,
               ending point latitude, ending point longitude, Check_distance);

 if( Check_distance[0] > (double)circ_rad){//circ_rad is the radius of the circle
                String status=user+"went outside GeoFence at: "+currentDateTimeString;
                addNotification(status);
                try {
                    connection.publish("iotm/tracking",status.getBytes(),QoS.AT_LEAST_ONCE, false);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                String status=user+" is inside GeoFence at time :"+currentDateTimeString;

                try {
                    connection.publish("iotm/tracking",status.getBytes(),QoS.AT_LEAST_ONCE, false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

Put the above code in onLocationchanged function..!!