Create Get 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 :-

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<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 :-

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
android:text="CURRENT LOCATION" 
android:textAppearance="?android:attr/textAppearanceLarge" 
android:layout_width="214.5dp" 
android:layout_height="wrap_content" 
android:id="@+id/textView1" 
android:layout_gravity="center_horizontal" /> 
 <TextView 
android:text="Latitude" 
android:textAppearance="?android:attr/textAppearanceLarge" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/textView2" /> 
<TextView 
android:textAppearance="?android:attr/textAppearanceMedium" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/txtlatitude" /> 
<TextView 
android:text="Longitude" 
android:textAppearance="?android:attr/textAppearanceLarge" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/textView4" /> 
<TextView 
android:textAppearance="?android:attr/textAppearanceMedium" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/txtlong" /> 
</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 :-

 using System; 
 using Android.App; 
 using Android.Content; 
 using Android.Runtime; 
 using Android.Views; 
 using Android.Widget; 
 using Android.OS; 
 using Android.Locations; 
 using System.Collections.Generic; 
 using Android.Util; 
 using System.Linq; 
 namespace CurrentLocation 
 { 
 [Activity(Label = "CurrentLocation", MainLauncher = true, Icon = "@drawable/icon")] 
 public class MainActivity: Activity, ILocationListener 
 { 
 TextView txtlatitu; 
 TextView txtlong; 
 Location currentLocation; 
 LocationManager locationManager; 
 string locationProvider; 
 public string TAG 
 { 
 get; 
 private set; 
 } 
 protected override void OnCreate(Bundle bundle) 
 { 
 base.OnCreate(bundle); 
 // Set our view from the "main" layout resource 
 SetContentView(Resource.Layout.Main); 
 txtlatitu = FindViewById < TextView > (Resource.Id.txtlatitude); 
 txtlong = FindViewById < TextView > (Resource.Id.txtlong); 
 InitializeLocationManager(); 
 } 
 } 
 }

 

Step 7 :  Next go to MainActivity.cs below of OnCreate() here to declare InitializeLocationManager().In This Method to called in onCreate().

 

C# Code :-

 

 private void InitializeLocationManager() 
 { 
 locationManager = (LocationManager) GetSystemService(LocationService); 
 Criteria criteriaForLocationService = new Criteria 
 { 
 Accuracy = Accuracy.Fine 
 }; 
 IList < string > acceptableLocationProviders = locationManager.GetProviders(criteriaForLocationService, true); 
 if (acceptableLocationProviders.Any()) 
 { 
 locationProvider = acceptableLocationProviders.First(); 
 } else 
 { 
 locationProvider = string.Empty; 
 } 
 Log.Debug(TAG, "Using " + locationProvider + "."); 
 }

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

C# Code :-

 protected override void OnResume() 
 { 
 base.OnResume(); 
 locationManager.RequestLocationUpdates(locationProvider, 0, 0, this); 
 } 
 protected override void OnPause() 
 { 
 base.OnPause(); 
 locationManager.RemoveUpdates(this); 
 }

Step 9 : Finally go to OnLocationChanged().In This section to get Current Location Latitude and Longitude Information. This information will be assigned in Textview.

C# Code :-

public void OnLocationChanged(Location location)
{
currentLocation = location;
if (currentLocation == null)
{
//Error Message
} else
{
txtlatitu.Text = currentLocation.Latitude.ToString();
txtlong.Text = currentLocation.Longitude.ToString();
}
}

 

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

Download Source here

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

Leave a Comment

Your email address will not be published. Required fields are marked *