GeolocatorPlugin icon indicating copy to clipboard operation
GeolocatorPlugin copied to clipboard

GPS coordinates periodically don't update

Open TheForeignMan opened this issue 7 years ago • 2 comments

Bug Information

Version Number of Plugin: 4.0.1 Device Tested On: iPhone 6 & Samsung J5

Expected Behavior

New GPS coordinates on every request.

Actual Behavior

Calls to the code below return exactly the same coordinates as the previous requests, even after moving around and/or waiting around 20+ mins between readings.

Code snippet

    private async Task GetLocation()
    {
        isAcquiringPosition = true;
        LoadingDialogShow("Location", "Please wait. Acquiring current location...");

        Console.WriteLine("####### Checking location is enabled.");
        if (!CrossGeolocator.Current.IsGeolocationEnabled)
        {
            InfoDialogShow("Location", "Please turn on location services.");
            DismissLoadingDialog();
            return;
        }

        var result = await Plugin.Permissions.CrossPermissions.Current.RequestPermissionsAsync(new[] { Plugin.Permissions.Abstractions.Permission.Location });
        var status = result[Plugin.Permissions.Abstractions.Permission.Location];

        Console.WriteLine("####### Checking permissions.");
        if (status != Plugin.Permissions.Abstractions.PermissionStatus.Granted)
        {
            InfoDialogShow("Location", "Location is required for GPS. Device data cannot be saved.");
            DismissLoadingDialog();
            return;
        }

        CrossGeolocator.Current.DesiredAccuracy = 50;

        currentPosition = null;
        currentPosition = new Position();
        try
        {
            currentPosition = await CrossGeolocator.Current.GetPositionAsync(TimeSpan.FromSeconds(10));
            Console.WriteLine("####### GPS:: " + currentPosition.Latitude + ", " + currentPosition.Longitude);
        }
        catch
        {
            currentPosition = null;
            Console.WriteLine("####### GPS:: NON-EXISTENT");
        }

        if (currentPosition == null)
        {
            InfoDialogShow("Location", "Could not get GPS coordinates and determine current location.");
            DismissLoadingDialog();
            return;
        }

        gpsDataLatLong[0] = currentPosition.Latitude;
        gpsDataLatLong[1] = currentPosition.Longitude;

        DismissLoadingDialog();
        isAcquiringPosition = false;
    }

TheForeignMan avatar Aug 03 '17 08:08 TheForeignMan

@TheForeignMan that's rather strange! I am not able to reproduce on my Galaxy S7. Could you run the code on simulator, and manually try set the location?

prashantvc avatar Sep 28 '17 07:09 prashantvc

@prashantvc I'm also having this issue (iOS only). It only happens in some cases for a few minutes and it's really hard to reproduce. I did some reading and found out that CLLocationManager will sometimes cache the user's location, so you should always validate the timestamp provided by CLLocation to ensure fresh data.

CLLocationManagerDelegate

Core Location may report a cached value to your delegate immediately after you start the service, followed by a more current value later. Check the time stamp of any data objects you receive before using them.

LuizGsa21 avatar Dec 18 '17 09:12 LuizGsa21