Infinite Loop

Reverse Geocoding without Google’s Restrictions

Since iOS 3.0 it has been very easy for iPhone App developers to provide users with detailed information about their current location by using the built-in MKReverseGeocoder class. The MKReverseGeocoder class acts as an Objective C wrapper around Googles geocoding services and handles all the network activities as well as decoding of the returned result.

This all sounds very nice, except that you are only allowed to use the MKReverseGeocoder class if you also display the result in conjunction with a Google map. This may be perfectly fine for most Apps needing reverse geocoding but will effectively prevent other types of Apps from using this service. MyAlmanac is one of those Apps that fall into the latter category.

GeoNames Geocoding Services

Fortunately GeoNames provides an alternative to Google and with much less restrictive license terms. Basically all they require is that you give proper credit to them.

GeoNames provides a multitude of different geocoding related services most of which are accessible using XML and JSON. In MyAlmanac we’re making use of two of these services:

“findNearby” is a reverse geocoding service that will return the name of the nearest toponym or locality given a latitude and longitude. Much like the built-in MKReverseGeocoder does. It does not provide you with the street name and number, but there are other services available for that. Besides for Apps like MyAlmanac it’s not really important whether you’re at a certain street address when it comes to calculation of sunrise and sunset.

The other service MyAlmanac is currently making use of is the “search” service. Based on the provided search criteria it will return a number of matching locations along with various information such as time zone, country, county, etc.
[AdSense - blog banner]

ILGeoNames Wrapper Classes

We have implemented a Objective C wrapper class called ILGeoNamesLookup in order to easily deal with the handling of HTTP and JSON. ILGeoNamesLookup provides methods for both the “findNearby” and “search” services.

We have also implemented a search controller called ILGeoNamesSearchController that makes it extremely easy to provide search facilities in iPhone Apps.

Both of these classes are currently being used in MyAlmanac to provide the various geocoding services. We’re now making these classes available to the public as Open Source under the terms of the BSD license.

The files can be downloaded from GitHub including a sample app that shows how to use the classes. You are also more than welcome to provide contributions, fixes, suggestions, etc. on these classes.

In the next tutorial I will go into more details about how these classes work and how you may use them in your own iPhone Apps.

Comments (2) | Trackback

2 Responses to “Reverse Geocoding without Google’s Restrictions”

  1. Mark Johnson says:

    Its also pretty easy to do reverse geo lookup completely in app with no network calls. Geonames global locations database can be downloaded and written into a simple SQLite database. If you construct the right indexes and the right search SQL you can find the nearest city to your location very quickly. Ping me if you’d like details.

    • Claus Broch says:

      Thanks for the comments Mark.
      I’ve been looking into providing offline search for MyAlmanac and have managed to hack a small script together that can convert the geonames location database to SQLite.
      I did however notice that this database tends to grow quite large unless you very carefully select the minimum city size as well as which fields you need included.
      If you have some experience in this I would be happy to hear more about it.

Leave a Reply