Introduction
Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS).
In the Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available. Use Geocoder to get an address from a latitude and longitude.
Prerequisites
- Visual Studio 2015 Update 3.
The steps, mentioned below, are required to be followed in order to Reverse Geocode 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).
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.
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 source. Choose the Designer Window, if you want to design and you can design your app.
Step 4
After opening main.axml, file will open the main page designer. You can design the page, as per your desire.
Now, delete the Default hello world button. Go to the SourcePanel and you can see the button coding. You need to delete it. Afterwards, delete the XAML code, now delete the 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.
Step 6
You need to drag and drop the TextView.
Step 7
Now, go to the properties Window. You need to edit the button's Id value and text value (EX: android:id="@+id/revGeocodeButton" android:text="@string/revGeocodeButtonText").
Step 8
Also, edit the TextView Id value (Ex: android:id="@+id/addressText").
Step 9
In this step, go to Main.axml page SourcePanel. Note, the button's Id value and text value. Also, note Textview Id value.
Main.axml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:id="@+id/revGeocodeButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/revGeocodeButtonText"
- android:layout_marginBottom="10dp" />
- <TextView
- android:id="@+id/addressText"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- </LinearLayout>
Step 10
In this step, open String.xml page. Go to the Solution Explorer-->Resource-->values-->String.xml.
Step 11
After opening String.xml file, write XML code, mentioned below.
String.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="revGeocodeButtonText">Reverse Geocode Location</string>
- <string name="ApplicationName">reversegeocode</string>
- </resources>
Step 12
In this step, go to the MainActivity.cs page in Solution Explorer. Add Namespaces, mentioned below.
MainActivity.cs
- using Android.Locations;
- using System.Linq;
Step 13
In this step, go to MainActivity.cs page. Write the code, mentioned below between OnCreate() Method.
MainActivity.cs
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
-
- SetContentView(Resource.Layout.Main);
- var button = FindViewById < Button > (Resource.Id.revGeocodeButton);
- button.Click += async(sender, e) =>
- {
- var geo = new Geocoder(this);
- var addresses = await geo.GetFromLocationAsync(42.37419, -71.120639, 1);
- var addressText = FindViewById < TextView > (Resource.Id.addressText);
- if (addresses.Any()) {
- addresses.ToList().ForEach(addr => addressText.Append(addr + System.Environment.NewLine + System.Environment.NewLine));
- } else {
- addressText.Text = "Could not find any addresses.";
- }
- };
- }
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 Run option.
Output
After a few seconds, the app will start running on your phone. You will see the Reverse Geocode address is successful.
Summary
This was the process of how to reverse geocode an address in Xamarin Android app, using Visual Studio 2015.