Location in Xamarin Forms

This articles explains how your xamarin forms mobile app can ask user to enable location services, obtain location permission and coordinates such as latitude, longitude and altitude on Android and iOS devices. It also has few other things I have learned while working with location in Xamarin Forms.

I have created a sample application to make it easier to understand the steps required before your xamarin app can have access to user's location. This app uses platform specific code and Xamarin Essentials to get geolocation.

demo app
Screenshot of demo app

Step 1: Configuration

Before getting the location information, its important to configure the project.

Android

Android needs to know what type of permission the app could request from user. Depending upon your requirements, you may need to add few of these permissions in your AndroidManifest.xml file.

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

iOS

iOS shows the following string when requesting location permission from app user. Make sure you replace it with the your reason for getting location information from user and add these in your info.plist file.

    <key>NSLocationAlwaysUsageDescription</key>
    <string>Replace this with your reason for requesting location</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Replace this with your reason for requesting location</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Replace this with your reason for requesting location</string>

Step 2: Location Services

Second step to get location information is to make sure location services are enabled on user's mobile device. The following platform specific code will tell you whether you need to request user to enable them.

Android

    public bool IsLocationServiceEnabled()
    {
        LocationManager locationManager = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
        return locationManager.IsProviderEnabled(LocationManager.GpsProvider);
    }

iOS

    public bool IsLocationServiceEnabled()
    {
        return CLLocationManager.LocationServicesEnabled;
    }

If the location services are not enabled then you would need to request user to enable them. Unfortunately there is no easy way to prompt the user to enable location services programmatically but instead you can programmatically open device location settings page where user can toggle the switch to enable them, however this only works on Android.

Android

    public bool OpenDeviceLocationSettingsPage()
    {
        var intent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings);
        intent.AddFlags(ActivityFlags.NewTask);
        Android.App.Application.Context.StartActivity(intent);
        return true;
    }

Step 3: Location Permission

Your application need to request the user to grant it permission to access the location service. You could write that logic manually in Android and iOS code base but its easier to use Xamarin Essentials from Xamarin team because the app now can check and request for location permission from shared code.

The following code returns current permission status

    Xamarin.Essentials.Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

The following code requests the permission and returns status

    Xamarin.Essentials.Permissions.RequestAsync<Permissions.LocationWhenInUse>();

If the user denied the location permission request, you can try to request it again but sometimes operating system does not prompt the user to grant permission. In that case, the only way to request the user to grant permission is to redirect them to app settings page. Fortunately, Xamarin Essentials provide a way to do that.

    Xamarin.Essentials.AppInfo.ShowSettingsUI();

Step 4: Location Information

Once your application has access to location services and appropriate location permission, then last step is to actually get location Information. The following piece of code will return location information.

    Xamarin.Essentials.Geolocation.GetLocationAsync();

Thank You!

Thanks for reading. The source code for the sample application is available at github.