Xamarin Android: Create Android GPS Current Location App

Let’s start,

Step 1

Open Visual Studio, New Project, Templates, Visual C#, Android, then select Blank App (Android),

Give Project Name and Project Location.

Step 2 

Next go to Solution Explorer-> Project Name->AndroidManifest.xml open xml code and give the Permission for Location, Network, Coarse Location services.

XML Code

  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  2.   
  3. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  4.   
  5. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  6.   
  7. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />   

Step 3 - Next open Solution Explorer-> Project Name->Resources->layout->Main.axml click to open Design View.

Step 4 - Then Select Toolbar Drag and Drop Five Textview box.

 

 

XML Code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5. android:orientation="vertical"  
  6.   
  7. android:layout_width="fill_parent"  
  8.   
  9. android:layout_height="fill_parent">  
  10.     <TextView  
  11.   
  12. android:text="CURRENT LOCATION"  
  13.   
  14. android:textAppearance="?android:attr/textAppearanceLarge"  
  15.   
  16. android:layout_width="214.5dp"  
  17.   
  18. android:layout_height="wrap_content"  
  19.   
  20. android:id="@+id/textView1"  
  21.   
  22. android:layout_gravity="center_horizontal" />  
  23.     <TextView  
  24.   
  25. android:text="Latitude"  
  26.   
  27. android:textAppearance="?android:attr/textAppearanceLarge"  
  28.   
  29. android:layout_width="match_parent"  
  30.   
  31. android:layout_height="wrap_content"  
  32.   
  33. android:id="@+id/textView2" />  
  34.     <TextView  
  35.   
  36. android:textAppearance="?android:attr/textAppearanceMedium"  
  37.   
  38. android:layout_width="match_parent"  
  39.   
  40. android:layout_height="wrap_content"  
  41.   
  42. android:id="@+id/txtlatitude" />  
  43.     <TextView  
  44.   
  45. android:text="Longitude"  
  46.   
  47. android:textAppearance="?android:attr/textAppearanceLarge"  
  48.   
  49. android:layout_width="match_parent"  
  50.   
  51. android:layout_height="wrap_content"  
  52.   
  53. android:id="@+id/textView4" />  
  54.     <TextView  
  55.   
  56. android:textAppearance="?android:attr/textAppearanceMedium"  
  57.   
  58. android:layout_width="match_parent"  
  59.   
  60. android:layout_height="wrap_content"  
  61.   
  62. android:id="@+id/txtlong" />  
  63. </LinearLayout>  

Step 5 - Next open Solution Explorer, Project Name, MainActivity.cs.

Click to open Code Window.


Step 6

Next go to MainActivity.cs Add one Namespace and then go Mainactivity Class to add Interface for ILocationListener after add Interface that time will show some Events missing error then click to add Implement interface. After that Automatically create some methods.

 

  
 
 
C# Code

  1. using System;  
  2. using Android.App;  
  3. using Android.Content;  
  4. using Android.Runtime;  
  5. using Android.Views;  
  6. using Android.Widget;  
  7. using Android.OS;  
  8. using Android.Locations;  
  9. using System.Collections.Generic;  
  10. using Android.Util;  
  11. using System.Linq;  
  12. namespace CurrentLocation  
  13. {  
  14.     [Activity(Label = "CurrentLocation", MainLauncher = true, Icon = "@drawable/icon")]  
  15.     public class MainActivity: Activity, ILocationListener  
  16.     {  
  17.             TextView txtlatitu;  
  18.             TextView txtlong;  
  19.             Location currentLocation;  
  20.             LocationManager locationManager;  
  21.             string locationProvider;  
  22.             public string TAG   
  23.             {  
  24.                 get;  
  25.                 private set;  
  26.             }  
  27.             protected override void OnCreate(Bundle bundle)   
  28.             {  
  29.                 base.OnCreate(bundle);  
  30.                 // Set our view from the "main" layout resource  
  31.                 SetContentView(Resource.Layout.Main);  
  32.                 txtlatitu = FindViewById < TextView > (Resource.Id.txtlatitude);  
  33.                 txtlong = FindViewById < TextView > (Resource.Id.txtlong);  
  34.                 InitializeLocationManager();  
  35.             }  
  36.     }  
  37. }   

Step 7 - Then go to MainActivity.cs below of OnCreate() to declare InitializeLocationManager().This Method to call onCreate().

C# Code

  1. private void InitializeLocationManager()  
  2. {  
  3.     locationManager = (LocationManager) GetSystemService(LocationService);  
  4.     Criteria criteriaForLocationService = new Criteria   
  5.     {  
  6.         Accuracy = Accuracy.Fine  
  7.     };  
  8.     IList < string > acceptableLocationProviders = locationManager.GetProviders(criteriaForLocationService, true);  
  9.     if (acceptableLocationProviders.Any())  
  10.     {  
  11.         locationProvider = acceptableLocationProviders.First();  
  12.     } else   
  13.     {  
  14.         locationProvider = string.Empty;  
  15.     }  
  16.     Log.Debug(TAG, "Using " + locationProvider + ".");  
  17. }   

Step 8 - Next to create OnResume() and OnPause Methods.

 

C# Code

  1. protected override void OnResume()  
  2. {  
  3.     base.OnResume();  
  4.     locationManager.RequestLocationUpdates(locationProvider, 0, 0, this);  
  5. }  
  6. protected override void OnPause()  
  7. {  
  8.     base.OnPause();  
  9.     locationManager.RemoveUpdates(this);  
  10. }   

Step 9

Finally go to OnLocationChanged().This section to get Current Location Latitude and Longitude Information. That information will assign in Textview.
 

C# Code

  1. public void OnLocationChanged(Location location)  
  2. {  
  3.     currentLocation = location;  
  4.     if (currentLocation == null)  
  5.     {  
  6.         //Error Message  
  7.     } else   
  8.     {  
  9.         txtlatitu.Text = currentLocation.Latitude.ToString();  
  10.         txtlong.Text = currentLocation.Longitude.ToString();  
  11.     }  
  12. }   

Step 10 - Press F5 or Build and run the Application.

 

Finally, we have successfully created Xamarin Android GPS Current Location Application.

Up Next
    Ebook Download
    View all
    Learn
    View all