question

JassimAlRahma-9056 avatar image
0 Votes"
JassimAlRahma-9056 asked JessieZhang-2116 commented

Problem with Xamarin Essentials' Location

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


dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Did you get the permission of the location? please check it first.

0 Votes 0 ·

Yes I set the permission as explained here:

https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=ios




<key>NSLocationWhenInUseUsageDescription</key>
<string>Fill in a reason why your app needs access to location.</string>

0 Votes 0 ·

Is there any logs about this problem?

0 Votes 0 ·

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered JessieZhang-2116 commented

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JassimAlRahma-9056 ,I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·