Retrieve Geo Location Coordinates In Xamarin Forms Application Using Xamarin Essentials For Android And UWP

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,

  1. Windows 10 (Recommended)
  2. Visual Studio 2017
  3. 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

Retrieve Geo Location Coordinates In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

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)

Retrieve Geo Location Coordinates In Xamarin Forms Application Using Xamarin Essentials For Android And 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.

Retrieve Geo Location Coordinates In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 4

Add the Label and Button controls in Mainpage.Xaml for Title and displaying Latitude, Longitude, and Altitude.

  1. <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"/>  
  2. <Label FontAttributes="Bold" x:Name="lblLong" Text="Longitude :" VerticalOptions="Center" HorizontalOptions="Center"/>  
  3. <Label FontAttributes="Bold"x:Name="lblLat" Text="Latitude :" VerticalOptions="Center" HorizontalOptions="Center"/>  
  4. <Label FontAttributes="Bold"x:Name="lblAlt" Text="Altitude :" VerticalOptions="Center" HorizontalOptions="Center"/>  
  5. <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,

  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState);  

Also add the below method,

  1. publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
  2. {  
  3.    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  4.    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  5. }  

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,

  1. [assembly: UsesPermission(Android.Manifest.Permission.AccessCoarseLocation)]  
  2. [assembly: UsesPermission(Android.Manifest.Permission.AccessFineLocation)]  
  3. [assembly: UsesFeature("android.hardware.location", Required = false)]  
  4. [assembly: UsesFeature("android.hardware.location.gps", Required = false)]  
  5. [assembly: UsesFeature("android.hardware.location.network", Required = false)]  

In UWP,

For setting the location permission open the Package.appxmanifest in XamFormGeoLoc.UWP project and select the Capabilities tab and check Location.

Retrieve Geo Location Coordinates In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Step 7

Add the following namespace and code in Mainpage.Xaml.cs.

  1. using Xamarin.Essentials;  
  2. publicasyncvoid btn_clicked(object sender, System.EventArgs e) {  
  3.     try {  
  4.         var location = await Geolocation.GetLastKnownLocationAsync();  
  5.         if (location != null) {  
  6.             lblLat.Text += location.Latitude.ToString();  
  7.             lblLong.Text += location.Longitude.ToString();  
  8.             lblAlt.Text += location.Altitude.ToString();  
  9.         }  
  10.     } catch (FeatureNotSupportedException fnsEx) {  
  11.         // Handle not supported on device exception  
  12.     } catch (PermissionException pEx) {  
  13.         // Handle permission exception  
  14.     } catch (Exception ex) {  
  15.         // Unable to get location  
  16.     }  
  17. }  

Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormGeoLoc" x:Class="XamFormGeoLoc.MainPage">  
  3.     <StackLayout>  
  4.         <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" />  
  5.         <Label FontAttributes="Bold" x:Name="lblLong" Text="Longitude :" VerticalOptions="Center" HorizontalOptions="Center" />  
  6.         <Label FontAttributes="Bold" x:Name="lblLat" Text="Latitude :" VerticalOptions="Center" HorizontalOptions="Center" />  
  7.         <Label FontAttributes="Bold" x:Name="lblAlt" Text="Altitude :" VerticalOptions="Center" HorizontalOptions="Center" />  
  8.         <Button x:Name="btnclick" Text="Retrive Coordinates" Clicked="btn_clicked" />  
  9.     </StackLayout>  
  10. </ContentPage>  

Step 8

Deploy your App in UWP and Android. The output of the XamFormGeoLoc App is,

Retrieve Geo Location Coordinates In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
Retrieve Geo Location Coordinates In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
After clicking the Retrieve Coordinates Button,
 
Retrieve Geo Location Coordinates In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 
 
Retrieve Geo Location Coordinates In Xamarin Forms Application Using Xamarin Essentials For Android And UWP 

Summary

Now, you have successfully retrieved the device's current geolocation coordinates in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.


Similar Articles