About Location in xamarin forms

Asgar Ali 86 Reputation points
2022-05-26T14:59:41.78+00:00

How to find if Location is turned on or off in settings through code.
i have this xam.plugin. geolocator.
i use this code but it is not working.
IGeolocator Locator = CrossGeolocator.Current; protected override void OnAppearing() { base.OnAppearing(); if(Locator.IsGeolocationEnabled) GetLocation(); else DisplayAlert("Location","Please Turn on The Location","OK") } private async void GetLocation()
{
var Status = await CheckAndRequestLocationPermission();
if(Status == PermissionStatus.Granted)
{
var Location = await Geolocation.GetLocationAsync();
Locator.PositionChanged += Locator_PositionChanged;
if (!Locator.IsListening)
await Locator.StartListeningAsync(new TimeSpan(0,1,0),100);
LocationMap.IsShowingUser = true;
CenterMap(Location.Latitude, Location.Longitude); ;
}
}
`

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

1 answer

Sort by: Most helpful
  1. Asgar Ali 86 Reputation points
    2022-06-02T16:33:13.783+00:00

    i am using this code to check if Location is enabled on device.But it works for only Android.

    protected override void OnAppearing() 
            {
                base.OnAppearing();
                LocationManager manager = 
                LocationManager.FromContext(Android.App.Application.Context);
                bool b = manager.IsLocationEnabled;
                GetLocation();
    
            }