Skip to content
Richard Thombs edited this page Jul 5, 2017 · 2 revisions

Geocoding

Geocoding is the act of going from a string address such as "1600 Amphitheatre Parkway" to a pair of latitude and longitude values. I.e. 37.4230180, -122.0818530.

This is useful as a pair of latitude and longitude coordinates is a far more specific and accurate way of describing a location than using a string (although it is less human-readable).

Google Maps API for .NET (gmaps-api-net) provides access to Google's Geocoding API with a set of classes.

Examples

Geocoding a string

Let's suppose we want to search an address and get more information about it. We can write:

var request = new GeocodingRequest();
request.Address = "1600 Amphitheatre Parkway";
request.Sensor = false;

var response = new GeocodingService().GetResponse(request);

The GeocodingService class submits the request to the API web service, and returns the response strongly typed as a GeocodeResponse object which may contain zero, one or more results. Assuming we received at least one result (you should check this normally), let's get some of its properties:

var result = response.Results.First();

Console.WriteLine("Full Address: " + result.FormattedAddress);         // "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA"
Console.WriteLine("Latitude: " + result.Geometry.Location.Latitude);   // 37.4230180
Console.WriteLine("Longitude: " + result.Geometry.Location.Longitude); // -122.0818530
Clone this wiki locally