Geolocation.GetLocationAsync always returns null

Pearl 21 Reputation points

I am trying to get current geolocation for the mobile device. But it always returns null. I tried not using cacellation token. In that case it freezes undefinitely(I waited for more than 5 min once) Currently i am testing using mobile phone with android 9. Please help its urgent.
Here is my code

   private async void DisplayLocation()
            CancellationTokenSource CTS = new CancellationTokenSource();
            var request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(10));
            var location = await Geolocation.GetLocationAsync(request, CTS.Token);
            if (location == null)
                lbl.Text = "Unable to record phone location. Please check.";
                lbl.Text = "Latitude: " + location.Latitude + " Longitude: " + location.Longitude;
        catch (Xamarin.Essentials.FeatureNotEnabledException fneEx)
            lbl.Text = fneEx.Message;
        catch (Exception ex)
            lbl.Text = ex.Message;

BTW, i read a thread on the same topic but, the solution didn't help at all. So Please help me with this
Thanks in advance

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,011 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Kyle Wang 5,516 Reputation points

    Hi ShwetaDeshpande-7668,

    Welcome to our Microsoft Q&A platform!

    I tested your code and the example provided in documentation Xamarin.Essentials: Geolocation. And both of them work fine.

    Before getting the location, please make sure you have allowed your app to access this device's location, then swipe down notification bar and enable the "Location" for your device.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Pearl 21 Reputation points

    I have turned the "location" on in the device while debugging it. But its not working either. Moreover, these are the permissions

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
    <uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER" />
    <uses-permission android:name="android.permission.LOCATION_HARDWARE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    and mainactivity.cs

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
        protected override void OnCreate(Bundle savedInstanceState)
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);