Multiple Serverside Geocoder - Alpha Release

NuGet Download: https://nuget.org/packages/Lucuma.GeoCode/

This is a serverside wrapper library to some common geocoding services that allow the conversion of an address/place into its latitude and longitude from the server (not client/javascript).  This library in its current form returns basic information (latitude and longitude) and only returns some basic information such as accuracy, address, etc when available or applicable.  It will also return a list of the ambiguous results when available.

It currently supports 6 free services (some require api key) and allows you to specify failovers in the event you get throttled.  Many projects and websites show maps and allow users to enter city,state and/or location to display them and it is a general requirement to geocode that information first.  If you need to do it on the server side, this library will help you.

If you find this project helpful, please rate it or provide some feedback on discussions tab.. It would be appreciated.

The current geocoding services that are supported are:

  • Google Gmap - The default service used since it doesn't require an api key
  • Open Streets Map - No Api key required
  • Bing - API key required
  • MapQuest - API key required
  • Yahoo Place Finder - API Key required - It seems this service is being deprecated for the Yahoo! BOSS Geoservice
  • Cloud Made - API Key required
  • Yahoo BOSS Geo Services - Using the yahoo boss geo service. This one is not free and requires a consumer key, consumer secret, and app id from yahoo as well as some payment information.  See link for details.

If you'd like me to add another service, log it in the discussion or issue tracker and I'll take a look.  Please include a link to the documentation.

This library is in beta.

A sample site that uses the geocoders: http://multigeocoder.azurewebsites.net/

Common Usage Scenarios:

Default Usage (Google GMap, no setup required):

double lat;
double lng;

IGeoCodeResult GeoCoderResult = new GeoCodeResult(); 
GeoCoderResult = g.GetCoordinates("Austin, TX");  // defaults to Google's GMap Service
if (GeoCoderResult.HasValue) {
  lat = Target.Latitude;
  lng = Target.Longitude;   
  Target.Locations;  // has a list of all locations if more than one returned
}

Specify a Different Service (Open Street Map):

double lat;
double lng;
IGeoCodeResult Target = new GeoCodeResult();

GeoCoder gc = new GeoCoder();
gc.AddProvider(new OpenStreetMap());  
Target = gc.GetCoordinates("Austin, TX");
if (Target.HasValue) {
  lat = Target.Latitude;
  lng = Target.Longitude;
}

Specify a Service that Requires a Key:

GeoCoder gc = new GeoCoder();
BingMapConfig bmc = new BingMapConfig()
   .SetKey("YOUR-BING-API-KEY");

gc.AddProvider(new BingMap(bmc)); 
IGeoCodeResult Target = new GeoCodeResult();
Target = gc.GetCoordinates("Austin, TX");

Use a backup service in case primary fails:

IGeoCodeResult Target = new GeoCodeResult();
// set google map to bad url so it fails
// we are only doing this to simulate an issue
GoogleGmapConfig gmc = new GoogleGmapConfig()
  .SetUrl("http://madps.googleapis.com/maps/api/geocode/json?address={0}&sensor=true"); 

GeoCoder gc = new GeoCoder()
  .AddProvider(new GoogleGmap(gmc))
  .AddProvider(new OpenStreetMap()); 

Target = gc.GetCoordinates("Austin, TX");
if (Target.HasValue) {
  Target.Latitude;  // do something with it
  Target.Longitude;
}

Use individual Service without fancy backup (You don't have to use the GeoCoder class):

OpenStreetMap o = new OpenStreetMap();
IGeoCodeResult Target = new GeoCodeResult();
Target = o.GetCoordinates("Austin, TX");

There are several areas that will change but overall it could be used in most simple scenarios where you just need to convert an address/location to latitude and longitude.

Please NOTE that some of these services require you to get a key in order to use them and that each library may or may not have some terms that govern the use of the library.  Please take care to read the terms of the service you are using so that you don't get banned from its use.

Last edited Nov 14, 2012 at 9:42 PM by sbosell, version 12

Comments

No comments yet.