Reputation: 851
I am using below codes to fetch the current location of a user:
public class GetCurrentLocations
{
private CancellationTokenSource _cancelTokenSource;
private bool _isCheckingLocation;
public async Task GetCurrentLocation()
{
try
{
_isCheckingLocation = true;
GeolocationRequest request = new GeolocationRequest(GeolocationAccuracy.High, TimeSpan.FromSeconds(10));
_cancelTokenSource = new CancellationTokenSource();
Location location = await Geolocation.Default.GetLocationAsync(request, _cancelTokenSource.Token);
if (location != null)
Preferences.Default.Set("latitude", location.Latitude);
Preferences.Default.Set("longitude", location.Longitude);
Utility.DebugAndLog("Values:>>", $"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
}
catch (Exception ex)
{
// Unable to get location
Utility.SendCrashReport(ex);
}
finally
{
_isCheckingLocation = false;
}
}
public void CancelRequest()
{
try
{
if (_isCheckingLocation && _cancelTokenSource != null && _cancelTokenSource.IsCancellationRequested == false)
_cancelTokenSource.Cancel();
}
catch (Exception exception)
{
Utility.SendCrashReport(exception);
}
}
}
And I use it like below and send the location details to our server using an API. First I check the old location detail and new location details are same. If same no action and if not same I will send that location details to our server.
GetCurrentLocations getcurrentLocations = new GetCurrentLocations();
await getcurrentLocations.GetCurrentLocation();
string latitude = Preferences.Default.Get("latitude", "");
string longitude = Preferences.Default.Get("longitude", "");
string oldLatitude = Preferences.Default.Get("oldLatitude", "");
string oldLongitude = Preferences.Default.Get("oldLongitude", "");
if (latitude != oldLatitude && longitude != oldLongitude)
{
Preferences.Default.Set("oldLatitude", latitude);
Preferences.Default.Set("oldLongitude", longitude);
//API to save the new location details
}
else
{
//No action
}
On each 10 seconds I share the location details to our server and using these location details I draw a line on map using below code:
Polyline polyline = new Polyline
{
StrokeColor = Color.FromArgb("#ea4333"),
StrokeWidth = 5,
};
int step = Math.Max(1, validCoordinates.Count / 50);
for (int i = 0; i < validCoordinates.Count; i += step)
{
polyline.Geopath.Add(validCoordinates[i]);
}
map.MapElements.Add(polyline);
<maps:Map
x:Name="map"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
MapClicked="MapClicked"
IsScrollEnabled="True"
IsTrafficEnabled="False"
MapType="Street"
IsZoomEnabled="True">
</maps:Map>
My problem are:
This is working fine on some devices and in some devices the lines are not accurate.
In some devices the user is not moved but lines are showing. If the user is not moved, is there any location change happen?
Following are some of the inaccurate line screenshots and on all these cases the user is not moved.
Upvotes: 1
Views: 59