Forum Xamarin Xamarin.Forms

Save location changes every hour?

stesvisstesvis USMember ✭✭✭✭


one of my requirements is that from the office, admins must be able to check the user device gps coordinates and notify the user when that happens.

I am use App Center for push notifications, so my initial idea was to send a push notification "John Does just checked your location". Then when the notification is received, the phone would save the location via API.
The problem is that the "NotificationReceived" event is fired only when the user clicks on the notification, not when it's actually received (this sucks big time).

What else can I do?

I looked at the Geolocator plugin and how to listen for location changes and it seems like it could work, but i have a few questions.

  • How can I save the "last known location" every 60 minutes? I don't want to constantly save a new location every time it changes (the app is used by truck drivers and it would update constantly)
  • Is this going to drain the battery?



  • jezhjezh Member, Xamarin Team Xamurai
    edited March 2019

    You can try to use Timer to achieve this function.
    Since the CrossGeolocator_Current_PositionChanged update the position and saved the latest position to the variable Positions, we just need get the lastest Position every 60 minutes and save it.

        void savePostion() {
            System.Timers.Timer Timer1 = new System.Timers.Timer();
            Timer1.Interval = 3600000;
            Timer1.Enabled = true;
            //Timer1.Elapsed += OnTimedEvent;
            Timer1.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
                var pos = Positions.Last();
                Log.Warning("test","save position.............."+ pos.Latitude +" <---->" + pos.Longitude);
                     // you can save the postion here
                //Delete time since it will no longer be used.
  • jezhjezh Member, Xamarin Team Xamurai
    edited March 2019

    Since the function of saving the last position is not very Time-consuming and complex, though this will affect overall performance, but the impact on the battery is minimal.

    Hope it can help you.

  • stesvisstesvis USMember ✭✭✭✭

    Hey @jezh thanks for your answer.
    I believe the same can be achieved with the Geolocator Plugin.

    The first argument is basically the time interval between location checks:
    await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true);

    So I believe I could use it like this:
    await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromMinutes(60), 10, true);

Sign In or Register to comment.