Search code examples
xamarinxamarin.formsgeolocationgeofencing

Xamarin Forms - Geofencing library


I have created a xamarin mobile application and want to create the following feature: Create one or more geofences. When the user with the phone is at the location, I want to trigger some code. Are there any libraries that I can use to implement this cross platform with xamarin forms?


Solution

  • If your Geofence is a Circular Shape, you can use Xamarin.Essentials to get the distance between 2 points, and if the Distance is Smaller than the Geofence Distance, it's inside the Geofence.

    long geofenceDistance = 2;
    Location geofenceCentercordinate = new Location(centrerCoordinate.Latitude, centrerCoordinate.Longitude);
    Location userLocation = new Location(position.Latitude, position.Longitude);
    var distance = (long)Location.CalculateDistance(userLocation ,geofenceCentercordinate, DistanceUnits.Kilometers);
    if(distance < geofenceDistance){
        //it's inside the Geofence
    }
    

    Also, if you want constant listening of your location you can check my other anwser Here.