4

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.

Sam
  • 2,972
  • 6
  • 34
  • 62

2 Answers2

1

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

HasiruN
  • 26
  • 3
0

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.

IvanPavliuk
  • 1,460
  • 1
  • 21
  • 16