I am using Xamarin.Essentials. When I try to get the last known location, the message about device location permission is shown.
If I deny the permission the PermissionException is caught.
How I can check for location and fired again the location permission message?
try
{
var location = await Geolocation.GetLastKnownLocationAsync();
if (location != null)
{
await this.Navigation.PushModalAsync(Nav_to_MAP);
}
}
catch (PermissionException pEx)
{
// if deny location
}
This issue was opened last year, this is the response from James Montemagno:
Right now it will request the permission for you based on how the system handles it. On iOS a permission can only be requested once and on Android, it can be multiple times. If the user declines you will get a permission denied exception.
You can use the Permission Plugin today to handle checking and requesting
https://github.com/jamesmontemagno/PermissionsPlugin
I will open a new proposal for permissions as they are a tad bit tricky.
So, You could use the Permissions Plugin for Xamarin to check for the permission before asking. like so:
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
{
await DisplayAlert("Need location", "Gunna need that location", "OK");
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
//Best practice to always check that the key exists
if (results.ContainsKey(Permission.Location))
status = results[Permission.Location];
}
if (status == PermissionStatus.Granted)
{
try
{
var location = await Geolocation.GetLastKnownLocationAsync();
if (location != null)
{
await Navigation.PushModalAsync(Nav_to_MAP);
}
}
catch (PermissionException pEx)
{
// if deny location
}
}
See the Docs on how to set it up
Related
When calling Permissions.RequestAsync<Permissions.LocationWhenInUse>() I do not get any result and the App is getting stuck. No matter, if the user accepts or denies the request.
I've set up MainActivity as described in the documentation:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
Now I'm trying to get the permissions the following way. But the RequestAsync call is just getting stuck and I never recieve an answer. Once the permission is granted and the app is being restarted, everything works as expected.
public async Task CheckPermissions()
{
var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
if (status != PermissionStatus.Granted)
{
status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}
if (status == PermissionStatus.Granted)
{
HasLocationPermissions = true;
}
else if (status != PermissionStatus.Unknown)
{
HasLocationPermissions = false;
}
}
A synchronous blocking call further up your call stack can cause this; e.g., .Wait(), .Result, or .GetAwaiter().GetResult(). This is the classic deadlock situation described on my blog.
Since you are writing a Xamarin application, you may find this technique helpful - it's a way to start an asynchronous operation (synchronously), show a "loading..." state, and then update when the asynchronous operation completes.
I am using the CrossGeolocator.Current.StartListeningAsync method in my xamarin app to be able to listen to location updates in background for iOS.
I am using essentials to request permissions.
On first run we get location permission (in app only) and location using essentials and then we use StartListeningAsync for ios to be able to track location if the app is in background or foreground.
When the callback is hit we get a popup saying this app uses background location and gives you the option to use it or change back to use in app only option. On selection of any option the callback never completes and subsequent code isnt run.
Here is the popup I get after I have permission for when in use and then start listening:
Popup on ios
On subsequent runs once permissions are set manually the callback works.
Xamarin Forms Version: 5.0.0.1931
Xamarin Essential Version: 1.6.1
Geolocator Plugin Version: 4.6.2-beta
Code example:
private async Task StartListening()
{
if (CrossGeolocator.Current.IsListening)
return;
try
{
var settings = new ListenerSettings
{
ActivityType = ActivityType.Other,
DeferLocationUpdates = true,
DeferralDistanceMeters = 15,
DeferralTime = TimeSpan.FromSeconds(10),
ListenForSignificantChanges = false,
PauseLocationUpdatesAutomatically = false,
AllowBackgroundUpdates = true,
};
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(15), 5, true, settings);
CrossGeolocator.Current.PositionChanged += PositionChanged;
CrossGeolocator.Current.PositionError += PositionError;
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
You need to make sure the permission is ok before starting to listen to location updates. Have a try with following code:
public async Task GetLocationAsync()
{
var status = await CheckAndRequestPermissionAsync(new Permissions.LocationAlways());
if (status != PermissionStatus.Granted)
{
// Notify user permission was denied
return;
}else{
await StartListening();
}
}
public async Task<PermissionStatus> CheckAndRequestPermissionAsync<T>(T permission)
where T : BasePermission
{
var status = await permission.CheckStatusAsync();
if (status != PermissionStatus.Granted)
{
status = await permission.RequestAsync();
}
return status;
}
Sometimes I have a case that,copying from one blob to input assets blob copies only 0bytes...
I am retrying after the first try and also delay it for 80sec, but nothing changes...
The file is video and its size is 340mb... If you guys need any information, I can reply it...
private async Task CreateInputAssetBlobAsync(UploadRequest request)
{
var cloudBlobContainer = new CloudBlobContainer(request.InputAssetStorageUri);
var blockBlob = cloudBlobContainer.GetBlockBlobReference(request.BlobName);
var storageCredentials = new Microsoft.Azure.Storage.Auth.StorageCredentials(_apiAccess.TempBlobAccountName, _apiAccess.TempBlobContainerKey);
var tempBlobContainer = new CloudBlobContainer(new Uri(_apiAccess.TempBlobContainerAddress), storageCredentials);
var tempBlockBlob = tempBlobContainer.GetBlockBlobReference(request.BlobName);
try
{
await blockBlob.StartCopyAsync(tempBlockBlob);
do
{
if (blockBlob.CopyState.Status == CopyStatus.Pending)
await Task.Delay(1000);
await blockBlob.FetchAttributesAsync();
}
while (blockBlob.CopyState.Status != CopyStatus.Success);
await blockBlob.FetchAttributesAsync();
if (blockBlob.Properties.Length <= 0)
{
Task.Delay(80000);
await blockBlob.StartCopyAsync(tempBlockBlob);
do
{
if (blockBlob.CopyState.Status == CopyStatus.Pending)
await Task.Delay(1000);
await blockBlob.FetchAttributesAsync();
}
while (blockBlob.CopyState.Status != CopyStatus.Success);
}
return;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
You may want to take a look at the Remarks section of the Azure Blob API here
https://learn.microsoft.com/en-us/rest/api/storageservices/copy-blob
There are several remarks to be aware of with StartCopy blob.
“The Blob service copies blobs on a best-effort basis."
“For a block blob or an append blob, the Blob service creates a committed blob of zero length before returning from this operation."
“For all blob types, you can call Get Blob or Get Blob Properties on the destination blob to check the status of the copy operation. The final blob will be committed when the copy completes."
If the copies in your storage account are never completing, I would advise opening a support ticket with the details on the storage account, time and operation ids, etc.
Hope that is somewhat helpful.
Note: you should not use
do
{
if (blockBlob.CopyState.Status == CopyStatus.Pending)
await Task.Delay(1000);
await blockBlob.FetchAttributesAsync();
}
while (blockBlob.CopyState.Status != CopyStatus.Success);
but
do
{
await Task.Delay(1000);
await blockBlob.FetchAttributesAsync();
}
while (blockBlob.CopyState.Status == CopyStatus.Pending);
otherwise you will get an infinite loop if the copy has an error.
May be this is what happening in your case ?
In my code that works, I see that I retrieve a new CloudBlockBlob object once the copy is launched. Add the second line :
await tempBlockBlob.StartCopyAsync(blockBlob);
tempBlockBlob = (CloudBlockBlob)await tempBlobContainer.GetBlobReferenceFromServerAsync(request.BlobName);
and then do the fetchattribute and checks with tempBlockBlob. I got a 0 byte blob if I don't add the second line.
I'm building a Xamarin app and for the geolocation, I'm using the GeolocatorPlugin
The problem is that once the code wants to get the position, the code exists without warning.
My class fields:
private Position position;
private IGeolocator locator = CrossGeolocator.Current;
My page constructor:
public MainPage()
{
InitializeComponent();
locator.PositionChanged += Locator_PositionChanged;
locator.PositionError += Locator_PositionError;
}
OnAppearing event is calling the getLocationPermission:
private async Task GetLocationPermission()
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.LocationWhenInUse);
if (status != PermissionStatus.Granted)
{
//Not granted, request permission
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.LocationWhenInUse))
{
// This is not the actual permission request
await DisplayAlert("Need your permission", "We need to access your location", "Ok");
}
// This is the actual permission request
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.LocationWhenInUse);
if (results.ContainsKey(Permission.LocationWhenInUse))
status = results[Permission.LocationWhenInUse];
}
//Already granted, go on
if (status == PermissionStatus.Granted)
{
//Granted, get the location
GetLocation();
await GetVenues();
await locator.StartListeningAsync(TimeSpan.FromMinutes(30), 500);
}
else
{
await DisplayAlert("Access to location denied", "We don't have access to your location.", "OK");
}
}
The permission is granted and gets to the GetLocation() method:
private async void GetLocation()
{
//var locator = CrossGeolocator.Current;
try
{
var myPosition = await locator.GetPositionAsync();
position = new Position(myPosition.Latitude, myPosition.Longitude);
}
catch (Exception ex)
{
throw;
}
if (position == null)
{
//Handle exception
}
}
Once the line is reached with locator.GetPositionAsync(), it stops. No exception is thrown, also the PositionError isn't raised.
I have no idea why, but in the beginning it worked once, never worked after that.
The location settings in de Android Emulator are as follow:
Based on my research, you did not acheved that Location Changes like this link
I wrote a demo about Location changes. This is running screenshot.
This is my demo
https://github.com/851265601/GeolocationDemo
I am working on Xamarin form app with andorid, UWP and Windows 8 project. I am using Geolocation plugin created by Jamesmontemagno to get the current device location. It is working fine in windows 8 and UWP but whenever I am trying to run it against the android device I keep getting task cancelled exception. I have checked all the permissions that are required as per suggestion but still no luck. My code to access location is below
protected override void OnAppearing()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100; //100 is new default
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
try
{
var position = locator.GetPositionAsync(timeoutMilliseconds: 60000).Result;
//var pp = helper.Setting.Location;
var Latitude = position.Latitude;
var Longitude = position.Longitude;
}
catch(Exception ex)
{
var exc = ex;
}
}
}
Below is an image for my settings for android manifest
For anyone else who gets a timeout even with the await, only on Android, and even though the device's Google Maps app works fine, you are probably running into this bug which only happens on certain Android devices, but quite a few of them at that.
The issue is an old one that Google has never fixed. The solution, and one possible reason the Google Maps app works fine, is to use Google Play Services' fused location provider.
Currently the Geolocator Plugin just uses the regular Android Location Provider, but James has mentioned that he would like to use the Fused provider at some point. I have yet to try the fused provider myself though.
Try using the await keyword like it is used in the original code:
try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync (timeoutMilliseconds: 10000);
Console.WriteLine ("Position Status: {0}", position.Timestamp);
Console.WriteLine ("Position Latitude: {0}", position.Latitude);
Console.WriteLine ("Position Longitude: {0}", position.Longitude);
}
catch(Exception ex)
{
Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
}
This should take care that there are no race condition and therefore TaskCancellationException.
Thanks to #Radinator below is the working solution.
protected async override void OnAppearing()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100; //100 is new default
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
try
{
await SetLocation();
}
catch (Exception ex)
{
var exc = ex;
}
}
}
private async Task SetLocation()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100; //100 is new default
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
try
{
var position = await locator.GetPositionAsync(timeoutMilliseconds: 60000);
var Latitude = position.Latitude;
var Longitude = position.Longitude;
}
catch (Exception ex)
{
//log ex;
throw ex;
}
}
}
Faced 'Task killed' issue with v3.0.4. The following worked for me:
Uninstall the app
Update Geolocator to prerelease 4.0