Xamarin.Forms - Geolocation App

Introduction

In this article demonstrates how to access GPS hardware in your device in xamarin.forms application and that we can access our location of latitude and longitude.In this function not available in xamarin.forms.So,we need to install plugin for this.
 
Nuget package

Xamarin.Forms = search "Xam.Plugin.Geolocator"
            
Xam.Plugin.Geolocator

Simple cross platform plugin to get GPS location including heading, speed and more.Additionally, you can track geolocation changes, reverse geocode and more
 
 
Step 1

You can create xamarin.forms app by going to File >> New >> Visual C# >> Cross Platform >> Cross Platform App (Xamarin.Native or Xamarin.Forms), give the application name and click ok.

(Project name : LocationApp)

 
 
Step 2

Afterwards, add the following nuget packages for your projects.
  • Xam.Plugin.Geolocator
For that, open Solution Explorer and select your solution, right click and select "Manage Nuget Packages for the Solution".In the popup window, open "Browse" tab and browse "Xam.Plugin.Geolocator" and select the following nuget packages and select your projects then install it.



Step 3

After installed nuget packges, add button with clicked event and labels to MainPage.Xaml page.For that go to Solution Explorer >> LocationApp(PCL) >>MainPage.xaml and double click to open its design view.Here is the code for this page.
 
Used  toobox items
  • Button - get GPS location with clicked event
  • Label - longitude text label
  • Label - longitude label 
  • Label - latitude text label
  • Label - latitude label

Xaml code

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:LocationApp"  
  5.              x:Class="LocationApp.MainPage">  
  6.       
  7.     <StackLayout Orientation="Vertical">  
  8.                  <Button Text="Get GPS location"  
  9.                          Clicked="OnButtonClicked"  
  10.                          TextColor="Chocolate"  
  11.                          FontSize="30"/>  
  12.                  <Label Text="Longitude"  
  13.                        FontSize="60"  
  14.                         TextColor="BlueViolet"/>  
  15.                  <Label x:Name="LogitudeLabel"  
  16.                        FontSize="20"/>  
  17.                  <Label Text="Latitude"  
  18.                        FontSize="60"  
  19.                         TextColor="DarkRed"/>  
  20.                  <Label x:Name="LatitudeLabel"   
  21.                        FontSize="20"/>  
  22.     </StackLayout>  
  23. </ContentPage>  


Step 4

In this step, add namespace and cs code to MainPage.xaml.cs page.For that, open Solution Explorer >>LocationApp(PCL) >> MainPage.xaml.cs and click open MainPage.xaml.cs page .Here is the code.
 
namespace
  1. using Plugin.Geolocator; 
cs code
  1. using Plugin.Geolocator;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Xamarin.Forms;  
  8.   
  9. namespace LocationApp  
  10. {  
  11.     public partial class MainPage : ContentPage  
  12.     {  
  13.         public MainPage()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         private async void OnButtonClicked(object sender, EventArgs e)  
  19.         {  
  20.             var locator = CrossGeolocator.Current;  
  21.             locator.DesiredAccuracy = 50;  
  22.   
  23.             var position = await locator.GetPositionAsync(timeoutMilliseconds:10000);  
  24.   
  25.             LogitudeLabel.Text = position.Longitude.ToString();  
  26.   
  27.             LatitudeLabel.Text = position.Latitude.ToString();  
  28.   
  29.         }  
  30.     }  
  31.  
 
 
Step 5

Now, we need to add location permission to our projects
 
Android project

The ACCESS_COARSE_LOCATION & ACCESS_FINE_LOCATION permission are required. if users are running  Marshmallow the Plugin will automatically prompt them for runtime permissions.By adding this permissions Google Play will automatically fillter our devices without specific hardward.

Manually add this permission.For that, go to Solution Explorer >>LocationApp.Android >> right click our Android Soution >> select properties or enter ALT+ENTER button >> click open Android Manifest tab and click the following check boxs and save it.
  •  ACCESS_COARSE_LOCATION
  •  ACCESS_FINE_LOCATION
 
 
Universal Windows Platform project

You must set the Location permission.For that, open Solution Explorer >> open LocationApp.UWP >> double click open Package.appxmanifest >> go to "Capabilities" tab >> Followed by click Location checkbox and save it.
 
 
 
Step 6

Click 'F5' or 'Build' to run your projects. Running this projects, you will have the result like below.

 

Now press "GET GPS LOCATION'" button to get latitude and longitude.The final result like below,

 
Finally, we have successfullly created xamarin.forms location application.

Up Next
    Ebook Download
    View all
    Learn
    View all