Problem with Xamarin Essentials' Location

Jassim Al Rahma 1,521 Reputation points
2021-04-12T23:28:07.197+00:00

Hi,

In the below project, I am not able to get the location details, and lat and long shows zero.

https://www.jassimrahma.net/temp/LocationApp.zip

Kindly advice..

Thanks,
Jassim

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

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-04-16T07:35:28.827+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I found you didn't set the AndroidManifest.xml correctly.

    We should add the following inside of the manifest node:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    <uses-feature android:name="android.hardware.location" android:required="false" />  
    <uses-feature android:name="android.hardware.location.gps" android:required="false" />  
    <uses-feature android:name="android.hardware.location.network" android:required="false" />  
    

    For the code behind , you can refer to the following code :

       public partial class MainPage : ContentPage  
    {  
        bool isGettingLocation;  
    
        public MainPage()  
        {  
            InitializeComponent();  
        }  
    
        async void Button_Clicked(System.Object sender, System.EventArgs e)  
        {  
            isGettingLocation = true;  
    
            while (isGettingLocation)  
            {  
                var result = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.Default, TimeSpan.FromMinutes(1)));  
    
                resultLocation.Text += $"lat: {result.Latitude}, lng: {result.Longitude}{Environment.NewLine}";  
    
                await Task.Delay(1000);  
            }  
        }  
    
        void Button1_Clicked(System.Object sender, System.EventArgs e)  
        {  
            isGettingLocation = false;  
        }  
    }  
    

    Refer: Finding Your Way with the Xamarin.Essentials Geolocation API.

    And you can find the Sample code here: https://github.com/jfversluis/XFEGeolocationSample .

    Best Regards,

    Jessie Zhang


    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.