Geocode a street address

How to convert a street address to latitude and longitude coordinates

PDF for offline use
Sample Code:
Related Articles:
Related APIs:

Let us know how you feel about this

Translation Quality


0/250

This recipe shows how to geocode a user supplied street address to latitude and longitude coordinates by using the `Geocoder` class included in Xamarin.Forms.Maps.

Overview

The Xamarin.Forms.Maps NuGet package is used to add maps to a Xamarin.Forms app, and uses the native map APIs on each platform. This NuGet package provides the Geocoder class that converts between string addresses and latitude and longitudes.

By using the native map APIs on each platform Xamarin.Forms.Maps provides a fast, familiar maps experience for users, but means that some configuration steps are required to adhere to each platforms specific API requirements. For information about these configuration steps see Working with Maps in Xamarin.Forms.

Geocoding a street address

In the code building the user interface for a page, import the Xamarin.Forms.Maps namespace and create an instance of the Geocoder class.

using Xamarin.Forms.Maps;
// ...
Geocoder geoCoder;

public GeocodePage ()
{
  InitializeComponent ();
  geoCoder = new Geocoder ();
}

Then for a user supplied street address call the GetPositionsForAddressAsync method on the Geocoder instance in order to asynchronously get a list of positions for an address.

var address = inputEntry.Text;
var approximateLocations = await geoCoder.GetPositionsForAddressAsync (address);
foreach (var position in approximateLocations) {
    geocodedOutputLabel.Text += position.Latitude + ", " + position.Longitude + "\n";

Summary

This recipe shows how to geocode a user supplied street address to latitude and longitude coordinates by using by the Geocoder class included in Xamarin.Forms.Maps.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.