How to call Google Geocoding service from C# code

I have one class library in C#. From there I have to call Google service & get latitude & longitude.

I know how to do it using AJAX on page, but I want to call Google Geocoding service directly from my C# class file.

Is there any way to do this or are there any other services which I can use for this.


You could do something like this:

string address = "123 something st, somewhere";
string requestUri = string.Format("{0}&sensor=false", Uri.EscapeDataString(address));

WebRequest request = WebRequest.Create(requestUri);
WebResponse response = request.GetResponse();
XDocument xdoc = XDocument.Load(response.GetResponseStream());

XElement result = xdoc.Element("GeocodeResponse").Element("result");
XElement locationElement = result.Element("geometry").Element("location");
XElement lat = locationElement.Element("lat");
XElement lng = locationElement.Element("lng");

You will also want to validate the response status and catch any WebExceptions. Have a look at Google Geocoding API.

I don't have the reputation to comment, but just wanted to say that Chris Johnsons code works like a charm. For anyone as stupid as me, the assemblies are:

using System.Net;
using System.Xml.Linq;

You can also use the HttpClient class which is often used with Asp.Net Web Api or Asp.Net 5.0.

You have also a http state codes for free, asyn/await programming model and exception handling with HttpClient is easy as pie.

var address = "paris, france";
var requestUri = string.Format("{0}&sensor=false", Uri.EscapeDataString(address));

using (var client = new HttpClient())
    var request = await client.GetAsync(requestUri);
    var content = await request.Content.ReadAsStringAsync();
    var xmlDocument = XDocument.Parse(content);


You can call the web service and work with the json/xml response.,+Mountain+View,+CA&sensor=true

this returns a json response which you can work with.

As you can read in the terms of usage, you are only allowed to use their web service if you show a google map.

Here you can find all information about the service:

To see how to handle the json response, take a look at this topic: Google Maps v3 geocoding server-side

EDIT: I just found this article:

I didn't test this, but maybe it works for you :)

EDIT2: The codeproject example doesn't seem to work :( the url mentioned there returns always the same, no matter what address point is the input... However you should try to work with the JSON or XML result anyway, as this is the best practice I would say.

It might be a bit too late for this answer, but for those still getting here looking for an easy way to use Google Geocoding API, I wrote this simple nugget called Guigle API and it can be used like this:

GoogleGeocodingAPI.GoogleAPIKey = "YOUR API KEY";
AddressResponse result = await GoogleGeocodingAPI.SearchAddressAsync("100 Market St, Southbank");


var result = await GoogleGeocodingAPI.GetCityFromCoordinatesAsync(11.1111, 22.2222);
var city = result.Item1;
var country = result.Item3;
  • One thing that I like about it is that you get full objects from the API responses, so you have easy access to everything and you can use Linq and other collection helpers to easily retrieve data from it. For example for addresses, you have these types and more:

    public class AddressResponse
        public List<Address> Results { get; set; }
        public string Status { get; set; }
    public class Address
        public List<AddressComponent> AddressComponents { get; set; }
        public string FormattedAddress { get; set; }
        public Geometry Geometry { get; set; }
        public string PlaceId { get; set; }
        public List<string> Types { get; set; }

So you can do something like:

var state = addressComponentes.FirstOrDefault(r => r.Types.Contains(AddressType.administrative_area_level_1.ToString()))?.ShortName;
  • It can also be used for Google Places API queries.
  • All methods have an overloaded option that you can pass your own HttpClient if you're going to make multiple calls so you can open/close it yourself.
  • Sorry it's not fully implemented/tested yet but for these basic searches it's totally working.

The methods are easy to use and have summaries with detailed explanation.

You can install it with the nuget command Install-Package Easyforce.GuigleAPI

If you just want the source code the project is available on GitHub. Feel free to make a PR for your contribution =)

It's an old question. But I had the same question today and came up with this as a solution: C# GeoCoding / Address Validation API (supports several providers including Google Maps).

IGeocoder geocoder = new GoogleGeocoder() { ApiKey = "this-is-my-optional-google-api-key" };

IEnumerable<Address> addresses = await geocoder.GeocodeAsync("1600 pennsylvania ave washington dc");

Console.WriteLine("Formatted: " + addresses.First().FormattedAddress);
// Formatted: 1600 Pennsylvania Ave SE, Washington, DC 20003, USA

Console.WriteLine("Coordinates: " + addresses.First().Coordinates.Latitude + ", " + addresses.First().Coordinates.Longitude);
// Coordinates: 38.8791981, -76.9818437

Here is the corresponding NuGet package :

Install-Package Geocoding.Google

This may not be much but I just want to make one improvement in @Chris Johnson code if you just want to get the lat and lng value in double form the var lat and var lng of chris johnson has value with this format < lat>...number...< /lat>

So if you pass the value is a string to get numeric value you have to do like this

  double latitude = Double.Pars(lat.Value)
 double longitude = Double.Pars(lng.Value)

Need Your Help

Bash sleep in milliseconds

bash timer sleep

I need a timer which will work with milliseconds. I tried to use sleep 0.1 command in script I see error message:

How to generate a permutations or combinations of n rows in m columns?

sql combinations

Does anyone know how to create a table with m columns from table of n rows where the values in columns of each row represent a different combination or permutation of values from the original table...