As many if not all of my readers know approved iPhone developers are under a heavy NDA. This prevents public code discussions and step-by-step tutorials. One of the applications I'm currently working on heavily depends on the current user's location. When I started diving into creating location based applications I naively assumed that iPhone's Core Location Framework would provide me with a rich API for getting country, city and street information. Well, that's not the case.
At the end of the day all Core Location Framework gives you are latitude and longitude as a 2D coordinate. The rest is up to you.
So in case you want to create a location based application you've got to do the reverse geocoding yourself. There are many free web services out there which provide you with the latitude and longitude information if you input an address. That's not what we need. We do need it vice versa. Therefore the term is coined reverse geocoding.
Even without the ability to go into code level discussions here, these hints might shorten your journey:
- For Europe I'm using the free geolocation web services at http://www.geonames.org/. You don't have to register, get an App Id or anything like that. Just use it and feel happy.
- For reverse geocoding the findNearbyPostalCodes method is just fine.
- Here's what I do
- Use Core Location to get latitude and longitude
- Use GeoNames REST API to get the postal code
- Use NSXML parser to asynchronously parse the GeoNames resulting XML document
- Use the <name> element's content to resolve the user's current city
Finally here is a nice overview of all the web services GeoNames offers.
Last note: iPhone Simulator always resolves to Cupertino (so GeoNames works fine for NA cities, too).