-1

I am working on a app based on google maps.

I added a circle to the map ( https://ibb.co/PhZy0t6 ) and I have been tring to detect if the user entered the circle.

The user is a marker I added with the user current location.

I tried to do like the following:

// Circle code:
        circle = mMap.addCircle(new CircleOptions()
                .center(new LatLng(addressLatLng.latitude, addressLatLng.longitude))
                .radius(70)
                .strokeColor(Color.RED)
        );


// The rest

        float[] distance = new float[2];

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

        if (distance[0] <= circle.getRadius()) {
            Toast.makeText(this, "User Enter Circle", Toast.LENGTH_SHORT).show();

        }

However it didn't worked..

Thanks for any help

Mr.Goomer
  • 73
  • 7
  • 1
    Does this answer your question? [Android: Simple way to make a geofence?](https://stackoverflow.com/questions/12028928/android-simple-way-to-make-a-geofence) – Axiumin_ Nov 08 '20 at 22:30

1 Answers1

0

So finally thanks to @Axiumin ,I did this:

@Override
public void onLocationChanged(Location location) {


    userLocationMarker.setPosition(new LatLng(location.getLatitude(), location.getLongitude()));


    Location userLocation = new Location("userLocation");
    userLocation.setLatitude(location.getLatitude());
    userLocation.setLongitude(location.getLongitude());

    Location circleLocation = new Location("circleLocation");
    circleLocation.setLatitude(circle.getCenter().latitude);
    circleLocation.setLongitude(circle.getCenter().longitude);


    float distance = userLocation.distanceTo(circleLocation);

    if (distance <= 70){ // If distance is less than 70 Meters


    }



}
Mr.Goomer
  • 73
  • 7