locked
How to achieve Geofencing in xamarin forms? RRS feed

  • Question

  • User364101 posted

    Hi Everyone, I need to achieve the Geofencing functionality for my app. Suppose there will be a certain location and a user is moving to that location. When the user is out of that particular location it will send a reminder or something else. I have gone through some sample projects- https://github.com/rdelrosario/xamarin-plugins/tree/master/Geofence in this project there are some plugins which are deprecates from Nuget. https://allancritchie.net/posts/introducingshiny The sample is not working.

    I didn't get any exact information. Can someone please help me out regarding this?

    help

    Tuesday, February 4, 2020 11:46 AM

Answers

  • User369979 posted

    Helped you solve the conflict between different packages due to the reference to Google Play Service. And add the code below in your manifest:

    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your api key" />
    <uses-library android:name="org.apache.http.legacy" android:required="false" />
    

    It runs properly on my side now:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, February 12, 2020 8:08 AM

All replies

  • User369979 posted

    If you want to consume GeofencePlugin, there're some descriptions here: https://github.com/domaven/xamarin-plugins/tree/master/Geofence Which could lead us to the newer supported plugin: https://github.com/CrossGeeks/GeofencePlugin But if you want to consume Shiny directly I'm afraid you need to contact the author. It seems he combined multiple plugins into this library and we couldn't separately modify part of it. What we can do is to implement the new plugin by ourselves.

    Wednesday, February 5, 2020 5:53 AM
  • User364101 posted

    @LandLu said: If you want to consume GeofencePlugin, there're some descriptions here: https://github.com/domaven/xamarin-plugins/tree/master/Geofence Which could lead us to the newer supported plugin: https://github.com/CrossGeeks/GeofencePlugin But if you want to consume Shiny directly I'm afraid you need to contact the author. It seems he combined multiple plugins into this library and we couldn't separately modify part of it. What we can do is to implement the new plugin by ourselves.

    I have tried https://github.com/CrossGeeks/GeofencePlugin to explore but not able to make it run. Can you please help me with a sample if possible?

    Wednesday, February 5, 2020 6:24 AM
  • User369979 posted

    I tried to make a sample based on the newer plugin. There is not too much difference between them. However, I found it won't check the location permissions on Android so I embed a permission plugin: https://github.com/jamesmontemagno/PermissionsPlugin to check its permission first:

    var result = await CrossPermissions.Current.RequestPermissionsAsync(new Plugin.Permissions.Abstractions.Permission[] { Plugin.Permissions.Abstractions.Permission.Location });
    
    if (result[Plugin.Permissions.Abstractions.Permission.Location] == Plugin.Permissions.Abstractions.PermissionStatus.Granted)
    {
        CrossGeofence.Current.StartMonitoring(new GeofenceCircularRegion("My Region", 18.4802878, -69.9469203, 52220)
        {
    
            //To get notified if user stays in region for at least 5 minutes
            NotifyOnStay = true,
            StayedInThresholdDuration = TimeSpan.FromMinutes(5)
    
        });
    }  
    

    Then the CrossGeofence works as expected. I attached my sample here.

    Wednesday, February 5, 2020 7:51 AM
  • User364101 posted

    @LandLu said: I tried to make a sample based on the newer plugin. There is not too much difference between them. However, I found it won't check the location permissions on Android so I embed a permission plugin: https://github.com/jamesmontemagno/PermissionsPlugin to check its permission first:

    var result = await CrossPermissions.Current.RequestPermissionsAsync(new Plugin.Permissions.Abstractions.Permission[] { Plugin.Permissions.Abstractions.Permission.Location });
    
    if (result[Plugin.Permissions.Abstractions.Permission.Location] == Plugin.Permissions.Abstractions.PermissionStatus.Granted)
    {
        CrossGeofence.Current.StartMonitoring(new GeofenceCircularRegion("My Region", 18.4802878, -69.9469203, 52220)
        {
    
            //To get notified if user stays in region for at least 5 minutes
            NotifyOnStay = true,
            StayedInThresholdDuration = TimeSpan.FromMinutes(5)
    
        });
    }  
    

    Then the CrossGeofence works as expected. I attached my sample here.

    Thank you so @LandLu for the sample. This works fine but what if I want to show the user on GoogleMap that if he is on a particular location or not. Like if the user is in out of the polygon or not.

    Thanks again @LandLu

    Wednesday, February 5, 2020 8:17 AM
  • User369979 posted

    Follow the old samples to build this one. They don't have too many differences as I said above. However, if you want to consume the Google Map feature. Do not forget to add the com.google.android.maps.v2.API_KEY in the manifest.

    Wednesday, February 5, 2020 8:22 AM
  • User364101 posted

    @LandLu thanks, I will try the above sample.But I have created a sample on Shiny, added all the classes along with packages. It runs without any error but there is a blank main page. I attached the sample can you please re-check the sample.

    Wednesday, February 5, 2020 9:18 AM
  • User369979 posted

    There're so many unclear things in your sample. I noticed you used Prism but the initial method looks like:

    await this.NavigationService.Navigate("Main/Nav/Welcome");
    

    The main page should be wrapped by the navigation page and I didn't find a "Welcome" page in your sample. Moreover, the better experience is to post one question for one thread. It's very difficult to find out why it displays a blank page from an existed complicated project.

    Wednesday, February 5, 2020 9:38 AM
  • User364101 posted

    @LandLu Actually I took the help from https://allancritchie.net/posts/introducingshiny sample and added all the resources what seems necessary for the sample.

    By the way I have created a sample based on https://github.com/CrossGeeks/GeofencePlugin But the thing is it is not working properly. Can you please check it? I have attached the sample.

    Wednesday, February 5, 2020 10:26 AM
  • User364101 posted

    @LandLu I am getting this error in android , do you have any idea? I have attached the sample , can you please check?

    Wednesday, February 5, 2020 1:10 PM
  • User369979 posted

    Helped you solve the conflict between different packages due to the reference to Google Play Service. And add the code below in your manifest:

    <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your api key" />
    <uses-library android:name="org.apache.http.legacy" android:required="false" />
    

    It runs properly on my side now:

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, February 12, 2020 8:08 AM
  • User396469 posted

    Hello, would you help me with an example of Geofence please, I am looking for documentation but I cannot find.

    Wednesday, August 26, 2020 10:56 PM