I am developing a Xamarin forms application and trying to use Xamarin.Essentials Geolocation to determine my device location for Android. I followed the steps from https://learn.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android however the results are completely inaccurate as I am currently in central Europe but the result latitude and longitude is 37.4219983333333 and -122.084 which is location of Google's headquarters i guess.
Here is how my AndroidManifest.xml looks like
<?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.workingwithmaps">
<uses-sdk android:minSdkVersion="21" />
<application android:label="WorkingWithMaps.Android">
<meta-data android:name="com.google.android.geo.API_KEY" android:value="MY_API_KEY" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<!-- Necessary for apps that target Android 9.0 or higher -->
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_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" />
</manifest>
My MainActivity.cs class:
[Activity(Label = "Resti", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Xamarin.FormsMaps.Init(this, savedInstanceState);
LoadApplication(new App());
}
}
And than I call it from ContentPage like this:
protected async override void OnAppearing()
{
Xamarin.Essentials.Location location = null;
var request = new GeolocationRequest(GeolocationAccuracy.Medium, TimeSpan.FromSeconds(10));
cts = new CancellationTokenSource();
location = await Geolocation.GetLocationAsync(request, cts.Token);
base.OnAppearing();
}
Any idea what I am doing wrong?
I was testing this functionallity on emulator and as i have found out thanks to Jason, the emulator doesn't have actual GPS capabilities, it uses a mock location by default.