The Geolocation class is available at Xamarin.Essentials API. It is used to retrieve the device's current geolocation coordinates. Android, iOS, and UWP offer unique operating system and platform APIs that developers have access to all in C# leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.
Reading this article, you can learn how to retrieve the device's current geolocation coordinates (Latitude, Longitude, Altitude) in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in cross-platform application development.
The following important tools are required for developing Xamarin Forms App,
- Windows 10 (Recommended)
- Visual Studio 2017
- Android API 19 or higher and UWP 10.0.16299.0 or higher.
Now we can discuss step by step app development.
Step 1
Open Visual studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give the Suitable Name for your App (XamFormGeoLoc) ->OK
Step 2
Select the Cross-Platform template as a Blank APP ->Set Platform as Android and UWP and code sharing strategy as .NET standard, Afterwards, Visual Studio creates 3 projects (Portable, Android, UWP)
Step 3
For adding a reference,
RightClick your solution (XamFormGeoLoc) and Select Manage NuGet Packages.
For adding Xamarin.Essentials Reference, choose Browse and search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.
Step 4
Add the Label and Button controls in Mainpage.Xaml for Title and displaying Latitude, Longitude, and Altitude.
- <Label FontAttributes="Bold" Text="Retrieve the device's current geolocation coordinates in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>
- <Label FontAttributes="Bold" x:Name="lblLong" Text="Longitude :" VerticalOptions="Center" HorizontalOptions="Center"/>
- <Label FontAttributes="Bold"x:Name="lblLat" Text="Latitude :" VerticalOptions="Center" HorizontalOptions="Center"/>
- <Label FontAttributes="Bold"x:Name="lblAlt" Text="Altitude :" VerticalOptions="Center" HorizontalOptions="Center"/>
- <Button x:Name="btnclick" Text="Retrive Coordinates" Clicked="btn_clicked"/>
Step 5
Add the following code in the MainActivity.cs in XamFormGeoLoc.Android project,
In onCreate() method,
- Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Also add the below method,
- publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
- {
- Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
- }
Step 6
For Retrieving GEO Coordinates,
In Android,
Add the following code to AssemblyInfo.cs file under the Properties folder for enabling Coarse and Fine Location permissions in XamFormGeoLoc.Android project,

Hrishikesh GarudPosted Nov 24, 2020, 5:02 AM
Why altitude is zero? and i have to specify altitude reference system how to do that?
Barath Kumar MPosted Sep 4, 2019, 4:39 AM
Thanks !!!,