xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.52k stars 505 forks source link

[Bug] GetLocationAsync returning old/non-current latitude and longitude #1315

Open Bejasc opened 4 years ago

Bejasc commented 4 years ago

Description

I'm having some trouble with (what appears to be) GetLocationAsync returning a cached result, or a result with a new timestamp, but old latitude and longitude values.

Coming from the old Geolocator plugin, I recall mention of only being able to get a location a few times each hour. However, the app is running in the foreground (on screen) when GetLocationAsync is performed. A non-current result is till returned.

I cannot find any such warning in current documetnation for the Essentials.Geolocation package.

I am unsure if this is a limitation of the device/package, or something missing in my implementation.

Regardless, I would like to be able to understand this issue, so I can report back to my Test and BA teams about the existence of any limitations.


Code snippet

    var request = new GeolocationRequest(GeolocationAccuracy.High);
    var loc = await Geolocation.GetLocationAsync(request);
    //Value of loc is serialized to Json string and processed by server

Steps to Reproduce

Below scenario was experienced on iPhone XS running iOS 13.5.1 No power saving settings were enabled. Wifi was disabled, 3G coverage was available. (It is a requirement of our app that its able to function on 3G without wifi, e.g remote and regional locations)

Position A and Position B refer to different physical areas, in my case, a few blocks down the street.

1. At 12pm, physically at Location A. Call GetLocationAsync. Located with my current physical latitude and longitude at Position A. This is current and correct.

2. At 12:10pm, at Location B. Call GetLocationAsync. Located with a new current physical latitude and longitude, at Position B. This is current and correct.

Both results are entirely unique with the Timestamp, Latitude, and Longitude, and considered correct and accurate.

3. At 12:20pm, now back at Location A. Call GetLocationAsync. A brand new result is produced, with a new timestamp. However, Latitude and Longitude are using values from the previous result (Location B), despite the device physically being over 1km away. as if it were cached, and "not different enough" to deem actually getting a new loc result?

4. Any calls placed through GetLocationAsync continue to report the old location, with a new timestamp.

  1. (Device fix) At 12:21pm, open Google Maps or other mapping/locating software on the device. The location the user is at briefly is represented as the old location - but soon updates to the current / correct physical device location

  2. Calling GetLocationAsync after opening Maps - the location is now correctly reported at Location A, and begins to work as expected.

Expected Behavior

GetLocationAsync to return... New location with brand new timestamp, new latitude, new longitude

Actual Behavior

GetLocationAsync is returning a new location - new timestamp, old latitude, old longitude.

Bejasc commented 4 years ago

For my own reference, this may be similar to these issues..

https://github.com/xamarin/Essentials/issues/900 https://github.com/xamarin/Essentials/issues/551

mina5500 commented 3 years ago

same here

charmosz commented 3 years ago

Hi. Same problem here reported by multiple users. Is there a fix ?

mina5500 commented 3 years ago

If you increase timeout to 10 seconds... it works fine... if you set to 1 second and the device didn't get the location it gets the last known location while it should return nothing

mesheets commented 2 years ago

Similar to #1678 ?

mesheets commented 2 years ago

While the steps to reproduce were on iOS, have also had numerous issue reports with this on Android.

In looking at the Android implementation, there are no calls to the underlying Android GetCurrentLocation(), only to GetLastKnownLocation(), which—based on some platform-specific testing—is a likely cause of this issue.

In Xamarin.Essentials.Geolocation, when calling Xamarin.Essentials.Geolocation.GetLocationAsync() [which is supposed to "returns the current location of the device"], the underlying Android API call should be to Android.Location.LocationManager.GetCurrentLocation() [and not Android.Location.LocationManager.GetLastKnownLocation()].

Android.Location.LocationManager.GetLastKnownLocation() should only be invoked when Xamarin.Essentials.Geolocation.GetLastKnownLocationAsync() is called.

mina5500 commented 2 years ago

@jamesmontemagno can you please help ?

AmrAlSayed0 commented 2 years ago

@mesheets This overload of getCurrentLocation is only available on API and this overload is only available for API 31 so they can't be use on all versions.

Also GetLastKnownLocation is only use when there is no appropriate provider for the accuracy you requested otherwise a listener for the location is set up to get the live location

mesheets commented 2 years ago

Yes, I am aware that one overload of GetCurrentLocation() was introduced in API level 30 and another in API level 31; however, there still is no exposing of this method of this method even if these newer API levels are in use.

To the main problem of this issue that has been reported, though, outdated location information is returned (and not the "live" location), so something is not working. In internal troubleshooting, making a call GetCurrentLocation() does force an update, but there is currently no way to use that via Xamarin.Essentials.

From the original post:

Expected Behavior

GetLocationAsync to return... New location with brand new timestamp, new latitude, new longitude

Actual Behavior

GetLocationAsync is returning a new location - new timestamp, old latitude, old longitude.