Taqi Gates
Taqi Gates

Reputation: 25

CrossGeolocator.Current.IsGeolocationEnabled returns false always

my requirement is to get latitude and longitude from gps, it was working find until I updated the NuGet packages and android sdk. Even if location services are on still I can't access the location.

var locator = CrossGeolocator.Current;

        if (locator.IsGeolocationEnabled)
        {               
                locator.DesiredAccuracy = 15;
                var position = await 
                locator.GetPositionAsync(TimeSpan.FromMilliseconds(3000));
                Latitude = position.Latitude;
                Longitude = position.Longitude;

        }

Here if statement returns always false.

My AndroidManifest looks like this..

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.ZameeliNew" android:installLocation="auto"> <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" /> <application android:label="ZameeliNew.Android" android:usesCleartextTraffic="true" android:icon="@drawable/iconBase"> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyBExspvDUbXly-g3YNQdUdnONRSEaHoiNw" /> <uses-library android:name="org.apache.http.legacy" android:required="false" /> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>

Upvotes: 0

Views: 954

Answers (1)

Adlorem
Adlorem

Reputation: 1517

You are probably using outdated repo with xamarin forms > 4.0. This project was moved to Xamarin.Essentials. I strongly recommend to use this one.

Then in your code you can use for example

    internal static async Task<Location> GetDeviceLocation()
    {
        try
        {
            var request = new GeolocationRequest(GeolocationAccuracy.Medium);
            var location = await Geolocation.GetLocationAsync(request);
            return location;
        }
        catch (Exception ex)
        {
            Helpers.ShowError("could not locate device: " + ex.Message, 6000);
        }

        return null;
    }

Don't forget to initialize component first.

To check device exceptions:

try
{
    var location = await Geolocation.GetLastKnownLocationAsync();

    if (location != null)
    {
        Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
    }
}
catch (FeatureNotSupportedException fnsEx)
{
    // Handle not supported on device exception
}
catch (FeatureNotEnabledException fneEx)
{
    // Handle not enabled on device exception
}
catch (PermissionException pEx)
{
    // Handle permission exception
}
catch (Exception ex)
{
    // Unable to get location
}

Upvotes: 1

Related Questions