0

There are lots of existing questions relating to this issue, but I have looked at as many of them as I could find and did not get an answer.

I'm trying to perform an offline reverse geocoding lookup on iOS based on a latitude and longitude. I'd like to be able to provide a latitude and longitude, and be provided with the country in which that point lies. I can do this with Geonames (such as: http://api.geonames.org/countryCode?lat=45.03&lng=8.2&username=demo), but I need a similar ability offline, without Internet functionality on the device.

CLLocation does not provide offline services that work reliably enough for what I'm doing, it relies on caches made while you were previously online, etc. Messy.

I've tried this: https://github.com/drodriguez/reversegeocoding but haven't had any luck, it requires some slightly complex / confusing Terminal installations using something called Thor which I've never heard of, and was throwing up a variety of errors, so I bailed on it.

I've found a few downloadable maps, but these seem to be even more complicated, and worryingly, hundreds of megabytes or even gigabytes in size – much beyond the scope of an iOS app. I only need countries, nothing smaller than that (cities, streets, locations, etc.) so I think I should be able to get a much smaller file.

So my key question is: is there some pre-existing database or tool, preferably with iOS support, that I can feed a latitude/longitude, and get a country? And, if not, what steps should I take to get such functionality working on my own?

Thanks in advance.

Luke
  • 9,512
  • 15
  • 82
  • 146
  • If that would be enough for what your app does, you can approximate the lookup with the locations of the airports (see http://www.openflights.org/data.html) or cities (http://en.wikipedia.org/wiki/List_of_cities_by_longitude) – Baglan Jan 29 '13 at 17:52

3 Answers3

2

ReverseGeocodeCountry is a simple lightweight offline country reverse geocoder for iOS, it has a static JSON file with country polygon data that is used to reverse geocode any lat/lng:

https://github.com/krisrak/ios-offline-reverse-geocode-country

krisrak
  • 12,882
  • 3
  • 32
  • 46
  • This looks very promising – this is an old project I was working on but you may have just given me a reason to re-visit it! :) – Luke Mar 05 '14 at 15:26
  • do you know about an offline "city" reverse geocoder or if you have a city polygon data – Ted Oct 28 '14 at 00:30
  • you have find data and replace in json file, i found US cities on google search, i'm sure its all out there – krisrak Oct 28 '14 at 03:43
1

The "Countries of the World" is a .csv text file with countries, coordinates, localised country names, capitals and other information. It seems to be free to use. You just have to import it into an SQLite database.

Edit Just noticed you want reverse geocoding. The database would only be good for forward geocoding.

You can download shapefiles for all countries at http://www.gadm.org/download. If you download a .kmz, you can unpack it to a list of coordinates for the borders. You could probably take every 5th or 10th coordinate to get smaller size (with less accuracy).

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • If it has maximum and minimum latitude and longitudes for all countries, it should do what I need, shouldn't it? I just throw those values into a data structure of some sort, then query it when the user taps on the map in my app? – Luke Jan 29 '13 at 17:54
  • It wouldn't be accurate enough for border regions. Think of France's border. You'll get huge sections of Belgium, Germany and The Netherlands in the North-East section. You're going to have to deal with shapefiles for country borders instead. – nevan king Jan 29 '13 at 17:59
  • Oh, so this will basically draw a rectangle around the country's most northern, southern, western, and eastern points? That might not be awful, there won't be much cause for people to be clicking around borders, they're choosing a country from the whole world. With regard to the shapefiles, how might I proceed there? I've literally never done anything like this be fore. – Luke Jan 29 '13 at 18:03
  • I added some detail to the answer. I've never actually used shapefiles, but you'll get a list of coordinates along the border. From there I'm afraid you're on your own. – nevan king Jan 29 '13 at 18:06
  • If you don't want to go to all this trouble, find the center coordinates for each country from the "Countries of the World", then present the user with a choice of 5-10 countries, ordered by distance. Most of the time their country will be in the first 3. – nevan king Jan 29 '13 at 18:09
  • That's not a bad idea, at least initally. I'll give these a go, thankyou. – Luke Jan 29 '13 at 18:15
1

Just in case I can suggest another good written offline geocoding library. https://github.com/Alterplay/APOfflineReverseGeocoding

Krivoblotsky
  • 1,492
  • 11
  • 17