jamesmontemagno / Xamarin.Plugins

Cross-platform Native API Access from Shared Code!
MIT License
1.3k stars 380 forks source link

GetPositionAsync locks the app #268

Closed pablo-gasparini closed 8 years ago

pablo-gasparini commented 8 years ago

Please take a moment to fill out the following (change to preview to check or place x in []):

This is a

Which plugin does this impact:

Version Number of Plugin: 3.0.4 Device Tested On: Samsung Galaxy S5, Lollipop 5.0.0 Simulator Tested On: N/A

Expected Behavior

the GetPositionAsync method should return the current device location

Actual Behavior

Plugin setup and permissions as indicated on the readme file but the plugin ignores the timeout and locks the app, you can see the "getting gps" icon on the drawer blinking and then locking but nothing happens after. Even after setting the accuracy to 100mts and the timeout to 100 seconds, nothing happens

Steps to reproduce the Behavior

public static async Task<Position> GetCurrentLocation()
{
    try
    {
        var locator = CrossGeolocator.Current;

        if (!locator.IsGeolocationAvailable)
            throw new NotSupportedException("Geolocation not available");
        if (!locator.IsGeolocationEnabled)
            throw new GeolocationException(GeolocationError.PositionUnavailable);

        return await locator.GetPositionAsync(timeoutMilliseconds: 100000);
    }
    catch (Exception ex)
    {
        //TODO: Add error logging
        return null;
    }
}
jamesmontemagno commented 8 years ago

How are you calling: GetCurrentLocation() ?

pablo-gasparini commented 8 years ago

Like this: Position coords = await GetCurrentLocation();

abreseeOpsec commented 8 years ago

Also experiencing this issue. Tested on the Xamarin Android Player Nexus 4 (Lollipop) image just now and it worked fine, but on a physical device (BLUE STUDIO SELFIE, Android 5.1) the call hangs forever.

My code is

public async void OnValidateButtonClicked(object sender, EventArgs ea)
{
    // cut irrevelant code
    var location = await CrossGeoLocator.Current.GetPositionAsync(10000);
    // cut irrelevant code
}

I've also tested using the PositionChanged event. On the Lollipop XAP image the event fires as expected. Same on my mashmallow physical device. My lollipop tester, however, never hits the breakpoint i set on that event handler.

abreseeOpsec commented 8 years ago

I should note that i'm not getting an app "lock" because of course i'm calling this code asynchronously. It just never returns, so the actions that are supposed to take place after getting the location don't take place.

I just did some testing and it appears this is occuring in the underlying Xamarin.Mobile library as well.

jamesmontemagno commented 8 years ago

are you try/catching it? if it times out it will throw an exception....

pablo-gasparini commented 8 years ago

yes I am, and I've left it running for around 15 minutes with nothing happening

jamesmontemagno commented 8 years ago

Did you implement the permissions requirement:

Android specific in your BaseActivity or MainActivity (for Xamarin.Forms) add this code:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) { PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults); }

I have had absolutely no issues with any of the 3.x releases.

pablo-gasparini commented 8 years ago

I added the required permissions for android on the manifest but I'll try your suggestion and see what happens. Thanks

abreseeOpsec commented 8 years ago

Yes, i have that in my MainActivity.cs. Things work fine on marshmallow -- would that OnPermissionsResult handler even be hit on lollipop?

I've also attempted try/catch. It doesn't ever throw an exception, no matter what time out value i pass

jamesmontemagno commented 8 years ago

Only effects Marshmallow all older versions just use the manifest, which I add automatically.

Do you have geolocation turned on on that device?

abreseeOpsec commented 8 years ago

Yes, I do. I was able to successfully get a location on the BLU STUDIO SELFIE using the fused location provider example -- https://developer.xamarin.com/samples/monodroid/FusedLocationProvider/

the normal location provider example did NOT work -- https://developer.xamarin.com/samples/monodroid/Location/

And i just cloned this repo and ran GeolocatorTests.Droid -- On my Nexus 5X test device, tapping the "Get Location" button gets the location just fine, but on the BLU STUDIO SELFIE it times out. Removing the timeout value from the test code leads to it hanging on "Getting..." forever.

e: we would switch to using the fused location provider component in our app (since it seems to work everywhere) but trying to add that component via nuget sent me into dependency hell

ferrydeboer commented 8 years ago

I was sort of experiencing similar issues. After checking the plugin it appeared that it was having a hard time giving me a location which was within the accuracy thresholds. I put a Debug.WriteLine in GeoLocationSingleListener.OnLocationChanged to see what accuracies I was receiving. If you're inside a building it might even have a hard time giving giving you a location with the default accuracy of a 100 meters.

shuaihuadu commented 8 years ago

I got an error: A task was canceled.

jamesmontemagno commented 8 years ago

Make sure your device/emulator have geolocation on. I have tested and used the plugin for a long time and it has been working on my devices if it can get a lock.

ruchi12l commented 7 years ago

@jamesmontemagno

Im still getting issue. Can you please help me to get out of it. It has been 2 days long and Im still stuck on it. When I check on lower versions(<6) it works great but on Marshmallow it stuck after I call GetLocationAsync(10000)

Device Location is on. I have requested for AccessFineLocation and AccessCoarseLocation on runtime. Dialog Appears, user can Deny/Allow. When I allow, It tries to get location which never returns .Never ever.Please help

IvanUpirov commented 7 years ago

Hello!

I've also faced this issue. I've implemented the permissions requirement in my MainActivity.cs and in Android manifest. Nevertheless, CrossGeolocator.Current.GetPositionAsync() newer returns when I test it on the emulator with Android 4.4.2 installed, but works absolutely fine on the same configurated emulator with Android 6.0. Geolocator plugin version: 3.0.4.0.

Are any changes pending with this method? Or it is considered as correct?

mattman624 commented 6 years ago

I had this issue, got it to go away somehow. Now trying to run unit tests in an android project and the problem comes back. This freezes the app. Doesn't even throw an error.

stvansolano commented 6 years ago

@jamesmontemagno might it have the sense to re-open this and consider @mattman624 case? I have recently faced the same issue here. Working fine on emulator but release app on real-device has issues and long-waiting call that never returns (like @ruchi12l mentioned) too.

JHCalasans commented 6 years ago

I'm having the same problem.

sgvictorino commented 6 years ago

Same problem here on an iOS 11.2 simulator.

jamesmontemagno commented 6 years ago

I don't respond to issues here. Go to the specific project or see: https://github.com/jamesmontemagno/GeolocatorPlugin/issues/210

u0107 commented 6 years ago

It was a simple solution really - I had switched off my location on my android because I did not want Google to track me... Switched it on and it works smoothly!

@jamesmontemagno - thank you - for a great piece of code!