How to find latitude and longitude using C#

MaxRecursion picture MaxRecursion · Jan 16, 2013 · Viewed 93.7k times · Source

I have a WCF service in C#.

In the Service call client sends a city name. I want to convert the city name to latitudes and longitudes and store in Database under demographics.

I am planning to use Google API to implement above functionality.

I have obtained an API key from Google and its of type 'Service account'.

How can I obtain the latitude and longitude using which APIs?

Do I need to install some SDK or any REST Service will do?

Answer

khellang picture khellang · Jan 16, 2013

You could try the NuGet package GoogleMaps.LocationServices, or just spin of its source code. It uses Google's REST API to get lat/long for a given address and vice versa, without the need for an API key.

You use it like this:

public static void Main()
{
    var address = "Stavanger, Norway";

    var locationService = new GoogleLocationService();
    var point = locationService.GetLatLongFromAddress(address);

    var latitude = point.Latitude;
    var longitude = point.Longitude;

    // Save lat/long values to DB...
}