Click here to Skip to main content
15,301,291 members
Articles / Programming Languages / C#
Posted 2 Nov 2008

Tagged as


59 bookmarked

GeoNames .NET WCF Client

Rate me:
Please Sign up or sign in to vote.
4.40/5 (16 votes)
21 Apr 2009MIT3 min read
A .NET WCF client for accessing the GeoNames services


GeoNames is a geographical database available under a Creative Commons attribution license. This data is accessible free of charge through a number (30+) of Web services.

The Web services include direct and reverse geocoding, finding places through postal codes, finding places next to a given place, weather observations for a given location, and finding Wikipedia articles about neighbouring places, etc.


I have recently been working with the GeoNames services, both from JavaScript and from .NET. Among other things, we have been working with postal code auto-completion on the Web using JavaScript, and server-side postal code validation using .NET.

All these cool GeoNames services could definitely be used in many different contexts and applications. I have some ideas myself of UI controls and widgets that I would like to develop using GeoNames as data provider.

Therefore, I developed this WCF client for accessing the GeoNames services from .NET in a uniform, easy object-oriented manner.

Using the Code

These are just a few C# examples of using the client.

All the services are available through a single class, the GeoNamesClient (inherits System.ServiceModel.ClientBase<T>). We first create an instance of this object:

using ( GeoNamesClient client = new GeoNamesClient ( ) )

Once the GeoNamesClient is created, any service may be called through this object. The first example shows how to retrieve all the cities within a geographical bounding box:

double north = 44.1, south = -9.9, east = -22.4, west = 55.2;
var cities = client.GetCities ( north, south, east, west );
foreach ( GeoCity city in cities )
  // GeoCity has many more properties to choose from...
  Console.WriteLine ( "City, Name={0}, Population={1}", city.Name, city.Population );

The next example shows how to find weather observation for a given location:

double latitude = 42, longitude = -2;
var response = client.FindNearbyWeather ( latitude, longitude );
GeoWeatherObservation weather = response.WeatherObservation;

// GeoWeatherObservation has many more properties to choose from...
Console.WriteLine ( "Current temperature is {0} degrees
  (delivered from the {1} weather station)",
  weather.Temperature, weather.StationName );

The client also provides overloaded methods for specifying culture, service verbosity, and controlling the max number of records to return for each service. The example shows how to search any geographical toponym that matches "london," with Spanish culture settings (place names, etc.) and limit the number of returned results to 10:

GeoToponymSearchCriteria criteria = new GeoToponymSearchCriteria ( );
criteria.Query = "london";
criteria.Culture = "es";
criteria.MaxRows = 10;
var response = client.SearchToponyms ( criteria );
foreach ( GeoToponym toponym in response )
  // GeoToponym has many more properties to choose from...
  Console.WriteLine ( "Found toponym match, name:{0}", toponym.Name );

Updated: Added support for geocoding addresses. This new example shows geocoding an address to determine it's spatial coordinate, and then pass coordinate to FindNearbyPostalCodes in order to find nearby postal codes for this address:

string address = "1600 Amphitheatre Parkway Mountain View, CA  94043";
double latitude, longitude;
if ( client.TryGeocode ( address, out latitude, out longitude ) )
  var response = client.FindNearbyPostalCodes ( latitude, longitude, 10 );

  foreach ( GeoPostalCode code in response )
  Console.WriteLine ( "-- Geocoding failed --" ); Update: Added support for approximating the bounding box for a given latitude/longitude pair, and a given radius in kilometers. The formula is based on the WGS84 system. A new useful GetBoundingBox method has been added to the GeoNamesClient class. This method could be used in conjunction with IGeoNamesClient methods that take bounding boxes, such as the GetCities method(s).

Below is an example of using GetBoundingBox to get a bounding box from a latitude/longitude pair and a radius, and then pass the bounding box to the GetCities method to retrieve cities within this bounding box:

double lat = 23.854, lng = 5.78;
double radius = 100; // kilometers
double north, south, east, west;

client.GetBoundingBox ( lat, lng, radius,
  out north, out south, out east, out west );

var cities = client.GetCities ( north, south, east, west );
foreach ( GeoCity city in cities )


If you need to access GeoNames services from your .NET application, you might want to look into my client. It supports all the available services, through a nice, object-oriented API.


  • 2008-11-02
  • 2008-11-04 Added support for geocoding
  • 2009-04-17
    • added support for WGS84 bounding box calculation (read updated article)
    • fixed Distance property serialization error, by changing type to String. The data returned in the Distance property is actually a String and not a Double; it is already formatted based on the specified return culture. Sorry for any existing code I might break..
    • fixed mapping to GeoStreetSegment.FeatureClasses, now mapped to MAF/TIGER Feature Class Codes
    • fixed/resolved failing tests


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Software Developer (Senior)
Norway Norway
Software Engineer at FAST, a Microsoft Subsidiary, in Oslo, Norway. Background in C++/PASCAL. Specialized in .NET and XML.

Comments and Discussions

Questionhelp on the webservice. Pin
macupryk5-May-13 10:09
Membermacupryk5-May-13 10:09 
QuestionReturn Null result Pin
Joe Yap25-Nov-11 2:29
MemberJoe Yap25-Nov-11 2:29 
QuestionHow to implement this code? Pin
shanNSK22-Oct-11 1:10
MembershanNSK22-Oct-11 1:10 
GeneralWould be awesome to see this as a NuGet package. Pin
danludwig29-Mar-11 17:02
Memberdanludwig29-Mar-11 17:02 
GeneralRe: Would be awesome to see this as a NuGet package. Pin
WernerCD16-Jul-14 9:46
MemberWernerCD16-Jul-14 9:46 
GeneralNew username requirement with Pin
JoelHales3-Feb-11 12:24
MemberJoelHales3-Feb-11 12:24 
GeneralPolicyException under Medium Trust Pin
Manzoni Fausto9-Dec-09 2:01
MemberManzoni Fausto9-Dec-09 2:01 
GeneralRe: PolicyException under Medium Trust Pin
baretta29-Dec-09 7:48
Memberbaretta29-Dec-09 7:48 
GeneralRe: PolicyException under Medium Trust Pin
Manzoni Fausto9-Dec-09 23:24
MemberManzoni Fausto9-Dec-09 23:24 
GeneralGetting Cities by PostCode [modified] Pin
mws-h13-Oct-09 4:25
Membermws-h13-Oct-09 4:25 
GeneralRe: Getting Cities by PostCode Pin
baretta213-Oct-09 6:21
Memberbaretta213-Oct-09 6:21 
GeneralRe: Getting Cities by PostCode Pin
mws-h15-Oct-09 21:29
Membermws-h15-Oct-09 21:29 
QuestionVB examples? Pin
dhalls20-Sep-09 9:02
Memberdhalls20-Sep-09 9:02 
GeneralBroken. :( Pin
Mike-EEEE28-Aug-09 6:27
MemberMike-EEEE28-Aug-09 6:27 
GeneralRe: Broken. :( Pin
baretta228-Aug-09 6:38
Memberbaretta228-Aug-09 6:38 
GeneralRe: Broken. :( Pin
Member 1466220520-Nov-19 2:25
MemberMember 1466220520-Nov-19 2:25 
GeneralGood Work, and suggestion Pin
l0t3k17-Apr-09 8:42
Memberl0t3k17-Apr-09 8:42 
GeneralRe: Good Work, and suggestion [modified] Pin
baretta217-Apr-09 12:14
Memberbaretta217-Apr-09 12:14 
GeneralError in using Web Service Pin
interface Mirror3-Apr-09 5:26
Memberinterface Mirror3-Apr-09 5:26 
GeneralRe: Error in using Web Service Pin
baretta23-Apr-09 6:26
Memberbaretta23-Apr-09 6:26 
GeneralRe: Error in using Web Service Pin
interface Mirror3-Apr-09 6:57
Memberinterface Mirror3-Apr-09 6:57 
GeneralRe: Error in using Web Service Pin
baretta23-Apr-09 7:20
Memberbaretta23-Apr-09 7:20 
GeneralDeserialization error [modified] Pin
Manzoni Fausto26-Mar-09 3:10
MemberManzoni Fausto26-Mar-09 3:10 
GeneralRe: Deserialization error Pin
baretta226-Mar-09 6:57
Memberbaretta226-Mar-09 6:57 
GeneralRe: Deserialization error Pin
baretta217-Apr-09 12:17
Memberbaretta217-Apr-09 12:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.