Forum Xamarin.Forms

How to use gps in API 23

Hello everyone.

I have been developing an app, which works locally with an API23, it must get the longitude and latitude in real time. I worked for a long time, but from time to time it does not work, however, I try it with an API24 and if it works for me. This is the code that I am using to test.

`private async void obtenerLatitudLongitud()
    {
        try
        {
            if (!CrossGeolocator.IsSupported)
                return;
            var position = await CrossGeolocator.Current.GetPositionAsync(TimeSpan.FromSeconds(20));
            await DisplayAlert("Prueba", position.Latitude.ToString() + " " + position.Longitude.ToString(), "OK");
        }
        catch (Exception ex)
        {
            await DisplayAlert("Error de Geolocalizacion", ex.ToString(), "OK");
            return;
        }
    }`

This is my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.GeolocalizadorXamarin" android:installLocation="preferExternal"> <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="25" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:label="GeolocalizadorXamarin.Android"></application> </manifest>

In the AssemblyInfo.cs add the following lines

[assembly: UsesFeature("android.hardware.location", Required = false)] [assembly: UsesFeature("android.hardware.location.gps", Required = false)] [assembly: UsesFeature("android.hardware.location.network", Required = false)]

This is the test with APIs 24 and 25

Tagged:

Answers

  • TinNguyen95TinNguyen95 CHMember ✭✭

    I have got the following code and it works like a charm. I use it on iOS, maybe there is something wrong with the permission, but im not quite sure about that.

        public async Task<Position> GetCurrentLocation()
            {
                try {
                    var locator = CrossGeolocator.Current;
                    locator.DesiredAccuracy = 50;
    
                    if (!locator.IsGeolocationEnabled || !locator.IsGeolocationAvailable) {
                        return null;
                    }
    
                    position = await locator.GetPositionAsync (TimeSpan.FromMilliseconds(1000));
    
                    return position;
                } catch {
                    // some catch code
                    return null;
                }
            }
    
  • ArnoldUlateArnoldUlate USMember ✭✭

    I was able to solve it, remove the following lines and everything worked again.

    [assembly: UsesFeature("android.hardware.location", Required = false)] [assembly: UsesFeature("android.hardware.location.gps", Required = false)] [assembly: UsesFeature("android.hardware.location.network", Required = false)]

Sign In or Register to comment.