简体   繁体   中英

How to use Xamarin Android LocationManager.GetCurrentLocation?

How can you use LocationManager.GetCurrentLocation with Xamarin.Android?

Specifically, I can't figure out how to use the IConsumer parameter required.

Here's the documentation for it:

https://docs.microsoft.com/en-us/dotnet/api/android.locations.locationmanager.getcurrentlocation?view=xamarin-android-sdk-12

EDIT :

If I leave my test device (Android 6) on my desk, next to a window, it'll only ever return null locations until I walk around with the device. Network reception is good, and if I return to the exact same location once it's "woken up" then GPS signal is good. This is true even for the lowest accuracy request.

Here's an example of the code I'm using (have tried various timeouts, and polling continuously):

Here's the code I'm using:

        // Get the last known location, from the OS cache
        Location cached = await Geolocation.GetLastKnownLocationAsync();

        // Location found?
        if (cached != null)
            OnLocationFound(cached);

        // Get current location
        GeolocationRequest networkRequest = new GeolocationRequest(GeolocationAccuracy.Lowest, timeout);
        Location network = await Geolocation.GetLocationAsync(networkRequest, cancellationToken.Token);

        // Location found?
        if (network != null)
            OnLocationFound(network);

        // Get current location
        GeolocationRequest gpsRequest = new GeolocationRequest(GeolocationAccuracy.Medium, timeout);
        Location gps = await Geolocation.GetLocationAsync(gpsRequest, cancellationToken.Token);

        // Location found?
        if (gps != null)
            OnLocationFound(gps);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM