Press ESC to close

Xamarin Forms Checking Location

You can find the current article here.

You may want to use location services in your mobile applications. For this, GPS must be turned on first. The user may forget it or we may want to get information from location services anywhere but the user may not know it. For this and other reasons, in this article I will tell you how to check if your location services are open in your mobile applications.

First we need to install a Nuget Package. To do this, you need to click on the right side of your project and click Manage Nuget Package. Then we need to include the Xam.Plugin.Geolocator package in the whole project in the browse section. The part up to here was simple.

We need to include the codes below in our project if we want to use it on the page later. In the scenario below I need to get the location of the user, the location. If I try to get location services directly, it will only wait if it is not open. I need to check this out. First we need to get a CrossGeoLocator object. I said this locator. Then I need to check it before doing anything. We can control it with isGeolocationEnabled and isGeolocationAvailable methods.

With isGeolocationAvailable we check the availability of the location. If it is being used by another application, location services will return it as false. With the isGeolocationEnabled function, we check whether location services are open. The next one is already getting location information. You can also find this in my earlier writings.

private async void GetLocation()
        {
            
            // Bir locator nesnesi oluşturup bunun arzulanan doğruluğunu saptamaya çalışıyorum
            var locator = CrossGeolocator.Current;
            if (locator.IsGeolocationEnabled && locator.IsGeolocationAvailable)
            {
                locator.DesiredAccuracy = 50;
                var position = await locator.GetPositionAsync();
            }
            
            
        }

If you have any questions, you can reach us by email or comment.

oomersezer@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *