How To Get the Geocode of An Address In Xamarin Android App Using Visual Studio 2015

Introduction

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS).

In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available. Geocoder is required to get a latitude and longitude for an address.

Prerequisites

  • Visual Studio 2015 Update 3.

The steps, mentioned below are required to be followed in order to get the Geocode of an address in Xamarin Android app, using Visual Studio 2015.

Step 1

Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).

Xamarin

Step 2

After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android).

Now, give your Android app; a name (Ex:sample) and give the path of your project. Afterwards, click OK.

Xamarin

Step 3

Now, go to Solution Explorer. In Solution Explorer, get all the files and sources in your project. Select Resource-->Layout-->double click to open main.axml page. To write XAML code, you need to select the source.

Choose the Designer Window, if you want to design, and you can design your app.

Xamarin

Step 4

After opening main.axml, file will open the main page designer. You can design the page, as per your desire.

Xamarin

Delete the Default hello world button. Go to the SourcePanel and you can see the button coding. You need to delete it.

After deleting XAML code, delete C# button action code.

Go to the MainActivity.cs page. You need to delete the button code.

Step 5

Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls.

You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls.

You need to drag and drop the button.

Xamarin

Step 6

You need to drag and drop the TextView.

Xamarin

Step 7

Now, go to the properties Window. You need to edit the button's Id value and text value

(EX: android:id="@+id/geocodeButton", android:text="@string/geocodeButtonText" ).

Xamarin

Step 8

Also, edit the TextView Id value

(Ex: android:id="@+id/addressText").

Xamarin

Step 9

In this step, go to the Main.axml page SourcePanel. Note, the button's Id value and text value. Also, note Textview's Id value.

Main.axml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  
  2.     <Button android:id="@+id/geocodeButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/geocodeButtonText" />  
  3.     <TextView android:id="@+id/addressText" android:layout_width="fill_parent" android:layout_height="fill_parent" />  
  4. </LinearLayout>  
Xamarin

Step 10

In this step, open the String.xml page. Go to the Solution Explorer-->Resource-->values-->String.xml.

Xamarin

Step 11

After opening String.xml file, write XML code, mentioned below.

String.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="geocodeButtonText">Geocode Address</string>  
  4.     <string name="ApplicationName">Geocodeaddress</string>  
  5. </resources>  
Xamarin

Step 12

In this step, go to the MainActivity.cs page in Solution Explorer. Add the Namespaces, mentioned below.

MainActivity.cs
  1. using Android.Locations;  
  2. using System.Threading;  
  3. using System.Linq;  
Xamarin

Step 13

In this step, go to the MainActivity.cs page and write the code, mentioned below between OnCreate() Method.

MainActivity.cs
  1. protected override void OnCreate(Bundle bundle) {  
  2.     base.OnCreate(bundle);  
  3.     // Set our view from the "main" layout resource  
  4.     SetContentView(Resource.Layout.Main);  
  5.     Button button = FindViewById < Button > (Resource.Id.geocodeButton);  
  6.     button.Click += (sender, e) => {  
  7.         new Thread(new ThreadStart(() => {  
  8.             var geo = new Geocoder(this);  
  9.             var addresses = geo.GetFromLocationName("Tiruvannamalai, Tamil nadu, india", 1);  
  10.             RunOnUiThread(() => {  
  11.                 var addressText = FindViewById < TextView > (Resource.Id.addressText);  
  12.                 addresses.ToList().ForEach((addr) => {  
  13.                     addressText.Append(addr.ToString() + "\r\n\r\n");  
  14.                 });  
  15.             });  
  16.         })).Start();  
  17.     };  
  18. }  
Xamarin

Step 14

If you have Android Virtual device, run the app on it, else connect your Android phone and run the app on it.
Simply, connect your phone and go to Visual Studio. The connected phone will show up in the Run menu.

(Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

Xamarin

Output

After a few seconds, the app will start running on your phone.

You will see the Geocode address.

Xamarin

Summary

This was the process of how to get the Geocode of an address in Xamarin Android app, using Visual Studio 2015. 


Similar Articles