Sam
Sam

Reputation: 3064

Check user is in particular radius or not (Flutter)

I want to check if the user is in his/her house using their Geolocation. The app will be running in the foreground and no map will be displayed. The user's house lat and long will be stored in the Firestore.

When the user opens the app, I want to check whether he/she is in his house. As house size may vary, I would consider it for few meter of radius.

Any help will be appreciated.

Upvotes: 4

Views: 5180

Answers (3)

Bobin
Bobin

Reputation: 445

There is an easy way:

MouseRegion(
    onHover: (event) => setState(() {
        Offset mousePosition = event.localPosition;
        var circle = Path()..addOval(Rect.fromCircle(center: Offset(0, 0), radius: 50));
        bool isHovered = circle.contains(mousePosition);
    })
)

There are 2 things which you must set yourself: the Offset and the radius of the circle.

The Offset should be the middle of your child widget: width / 2 and height / 2. You can get this with LayoutBuilder if you don't have the size.

The radius can be width / 2.

Upvotes: -2

IvanPavliuk
IvanPavliuk

Reputation: 1800

You can do like this:

double getDistanceBetweenPositions({
  required double homeLat,
  required double homeLong,
  required double userLat,
  required double useLong,
}) {
  final latDif = radians(homeLat - userLat);
  final longDif = radians(homeLong - useLong);
  final a = sin(latDif / 2) * sin(latDif / 2) +
      cos(radians(userLat)) *
          cos(radians(homeLat)) *
          sin(longDif / 2) *
          sin(longDif / 2);
  final b = 2 * atan2(sqrt(a), sqrt(1 - a));
  final result = earthRadius * b;
  return result;
}

If the distance is <= the house size (your radius), it means user is in the house.

Upvotes: 0

HasiruN
HasiruN

Reputation: 26

This is called geofencing. In this case, a few meters of radius around the house is the geofence.

You will be able to easily do this by using this flutter package. There are many other geofencing packages as well. Check on pub.dev

Upvotes: 1

Related Questions