1

I have two questions. In my app, I am getting the GPS coordinates of friends. How can I determine if those friends are in the US/Canada?

The second question is to find out if the actual device is in the US/Canada?

Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177

2 Answers2

3

The term for this is Reverse Geocoding. You want to have a look at the MKReverseGeocoder class and its sample code, this is Apple's solution for this. However, it requires you to display a map (which may or may not be what you want).

If you don't want the map then things get a little bit more complicated. See Offline Reverse Geocoding at Country Level and Techniques for offline reverse geocoding on a mobile device?.

Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • I looked at the class. I get the GPS coords from an address using it. How does that help determine if the person is in the US or another country like England, Egypt? – Cocoa Dev May 11 '11 at 13:07
  • @Cocoa Dev: You determine the GPS coords using CoreLocation, and then query the [placemark](http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKPlacemark_Class/Reference/Reference.html%23//apple_ref/doc/c_ref/MKPlacemark) using the MKReverseGeocoder class. The placemark tells you which country you're currently in. – DarkDust May 11 '11 at 13:28
  • Of course, note that this only works with a network connection. So you're probably out of luck in the cellar. But then you wouldn't get coordinates either (with no GPS and no WiFi). – DarkDust May 11 '11 at 13:42
  • CocoaDev, the solution I have provided should work. Try this option – RK- May 11 '11 at 13:48
  • I'm looking into the placemark. NSString *country = [placemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCountryCodeKey]; – Cocoa Dev May 11 '11 at 14:14
  • @Cocoa Dev: Why not simply use `placemark.country`, or maybe even better `placemark.countryCode` ? The later will always be the same, not matter which language the user is using (I'm not sure whether `placemark.country` is localized or not, Apple doesn't say). – DarkDust May 11 '11 at 14:41
1

You can use the Reverse Geocoding API of google to get the address corresponding to the coordinates and extract the country details from the response's address_components.

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true

RK-
  • 12,099
  • 23
  • 89
  • 155
  • 1
    The MKReverseGeocoder class uses Google maps. Also note that when you want to the [Google Maps Geocoding API](http://code.google.com/apis/maps/documentation/geocoding/) you need to obey to the [terms of service](http://code.google.com/apis/maps/terms.html). The most problematic one is this: *geocoding results without displaying them on a map is prohibited* – DarkDust May 11 '11 at 14:15
  • @DarkDust:+1.. Thanks for pointing out that. IMO, this particular condition makes no sense. – RK- May 12 '11 at 04:21
  • 1
    It does make sense because when you display the Google map, the term "Google" is overlayed and thus the user clearly sees where the data is coming from. So that's a kind of advertising for Google which they demand for using the service free-of-charge. I think it's a fair deal, although of course I'd also like to use the service without necessarily displaying a map. – DarkDust May 12 '11 at 06:42