Hi,
I am unable to captue the location Latitude and Longitude using GetPositionAsync. Sometimes it returns null.
If I enable location service from google map then application is getting the location Latitude and Longitude.
I had written the following permission in AndriodMainfest.xml
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> uses-permission android:name="android.permission.INTERNET" /> uses-feature android:name="android.hardware.location" android:required="true" /> uses-feature android:name="android.hardware.location.gps" android:required="true" /> uses-feature android:name="android.hardware.location.network" android:required="true" />
Following code has been witten th Android MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); Plugin.CurrentActivity.CrossCurrentActivity.Current.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); Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults); base.OnRequestPermissionsResult(requestCode, permissions, grantResults); }
Following code I had written to capture location in Xamarin shared file App.xaml.cs
using Plugin.Geolocator; protected override void OnStart() { try { var lblLat = ""; var lblLong = ""; var lblAlt = ""; var locator = CrossGeolocator.Current; locator.DesiredAccuracy = 100; var location = await locator.GetPositionAsync(TimeSpan.FromSeconds(10)); if (location != null) { lblLat += location.Latitude.ToString(); lblLong += location.Longitude.ToString(); lblAlt += location.Altitude.ToString(); } App.gblLatitude = lblLat; App.gblLongitude = lblLong; App.gblAltitude = lblAlt; return; } catch (FeatureNotSupportedException fnsEx) { return; } catch (PermissionException pEx) { return; } catch (Exception ex) { return; } }
I am using Visual Studio 2019 Xamarin Forms. How can I open the dialog box "to continue turn on device location which uses google's location service" when open the application.
Requesting your help.
Do you want to pop up the dialog box like following screenshot?
If so, please install following nuget packages for your xxxxx.Android project.
Then Add following code in your MainActivity.cs
using Android.App; using Android.Content.PM; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using Android.Gms.Location; using Android.Gms.Tasks; using Android.Gms.Common.Apis; using Xamarin.Forms; using static Android.Content.IntentSender; using Java.Lang; namespace App22.Droid { [Activity(Label = "App22", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { public static int REQUEST_CHECK_SETTINGS = 0x1; 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.Init(this, savedInstanceState); LoadApplication(new App()); MessagingCenter.Subscribe<App, string>(App.Current, "OneMessage", (snd, arg) => { Device.BeginInvokeOnMainThread(() => { LocationRequest mLocationRequest = new LocationRequest(); mLocationRequest.SetInterval(10); mLocationRequest.SetSmallestDisplacement(10); mLocationRequest.SetFastestInterval(10); mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(mLocationRequest); builder.SetAlwaysShow(true); var task = LocationServices.GetSettingsClient(Android.App.Application.Context).CheckLocationSettings(builder.Build()); task.AddOnCompleteListener(new MyOnCompleteListener(this)); }); }); } 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); } } class MyOnCompleteListener : Java.Lang.Object, IOnCompleteListener { private MainActivity mainActivity; public MyOnCompleteListener(MainActivity mainActivity) { this.mainActivity = mainActivity; } public void OnComplete(Task task) { //throw new NotImplementedException(); try { var response = task.GetResult(Java.Lang.Class.FromType(typeof(ApiException))); // All location settings are satisfied. The client can initialize location // requests here. }catch (ApiException exception) { switch (exception.StatusCode) { case LocationSettingsStatusCodes.ResolutionRequired: // Location settings are not satisfied. But could be fixed by showing the // user a dialog. try { // Cast to a resolvable exception. ResolvableApiException resolvable = (ResolvableApiException)exception; // Show the dialog by calling startResolutionForResult(), // and check the result in onActivityResult(). resolvable.StartResolutionForResult(mainActivity, MainActivity.REQUEST_CHECK_SETTINGS); } catch (SendIntentException e) { // Ignore the error. } catch (ClassCastException e) { // Ignore, should be an impossible error. } break; case LocationSettingsStatusCodes.SettingsChangeUnavailable: // Location settings are not satisfied. However, we have no way to fix the // settings so we won't show the dialog. // ... break; } } } } }
If I want to execute it in xamarin. forms. I used
MessagingCenter.Send<App, string>((App)App.Current, "OneMessage", "");
Thanks,
I have changed the Android compile version from 9.0 to 10.0(Q) and it is working fine.
Answers
Do you want to pop up the dialog box like following screenshot?
If so, please install following nuget packages for your xxxxx.Android project.
Then Add following code in your
MainActivity.cs
If I want to execute it in xamarin. forms. I used
Thanks for your support.
While implementing the same I am getting the following error
Could not find 10 Android X assemblies, make sure to install the following NuGet packages:
- Xamarin.AndroidX.Legacy.Support.V4
You can also copy-and-paste the following snippet into your .csproj file:
Following framework I am using
netstandard2.0
Please help.
You can search
Xamarin.AndroidX.Legacy.Support.V4
in the nuget solution like following screenshot.Based on your error, you need to install other nuget packages for migrating to AndroidX.
Thanks.
After installing the same following error is showing.
You need to install
xamarin.androidx.lifecycle.liveData
,xamarin.androidx.broaswer
andXamarin.Google.Android.Material
nuget packages. Here is an article about AndroidX migration in Xamarin.Forms, you can refer to it.https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/android/androidx-migration#automatic-migration-in-xamarinforms
Thanks,
I have changed the Android compile version from 9.0 to 10.0(Q) and it is working fine.