Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.

id title brief article sdk
Geocode an Address
This recipe shows how use the Geocoder to get a latitude and longitude for an address.


Follow these steps to geocode an address.

  1. In Main.axml (or whatever layout file you wish) add the following XML.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
  1. Add a string resource named geocodeButtonText to Strings.Xml.
<string name="geocodeButtonText">Geocode Address</string>
  1. Add a click event handler for the button in the Activity’s OnCreate method.
var button = FindViewById<Button> (Resource.Id.geocodeButton);
button.Click += (sender, e) => { … }
  1. Add the following code to the click event handler to call the Geocoder on a separate thread.
button.Click += (sender, e) => {
       new Thread (new ThreadStart (() => {
              var geo = new Geocoder (this);

              var addresses = geo.GetFromLocationName (
                      "50 Church St, Cambridge, MA", 1);

              RunOnUiThread (() => {
                      var addressText = FindViewById<TextView>

                      addresses.ToList ().ForEach ((addr) => {
                             addressText.Append (addr.ToString () +
                      } );
              } );
       } )).Start ();
} ;
  1. Running the application results in the address being displayed with location information as shown in the screenshot above.

Additional Information

This creates a Geocoder instance, which is in the Andorid.Locations namespace. The Geocoder calls GetFromLocationName with the address to geocode. When the result is returned, the code adds it to the UI inside a call to RunOnUiThread. The result of the Geocoder is an address list. In this call 1 address is returned because that is the number passed into the second argument of the GetFromLocationName call. The returned address contains a variety of information about the location, including the latitude and longitude.