5

I've looked around a bit on stackoverflow and Google Groups and haven't really found an answer for this exact question.

I simply want to be able to know what country the user of an Android phone is currently in.

I assume the TelephonyManager doesn't work if the user is using wifi only. Is this true?

We've tried passing the user's current latitude and longitude using android.location.Geocoder, but it's really flaky. It will frequently return empty results. Sometimes we can keep asking it and it will eventually return results, but it's really ugly. Is there a more reliable way?

Thanks, Rob Sz

Rob Szumlakowski
  • 947
  • 3
  • 8
  • 18
  • Maybe you can try using Google Maps API for getting the country by using latitude and longitude. And also http://gis.stackexchange.com/ will help in some way, i suppose. – Ranhiru Jude Cooray Oct 28 '10 at 15:30

3 Answers3

2

You could try reverse Geo Coding :)

http://code.google.com/apis/maps/documentation/geocoding/

Check this link. The country is returned in JSON.

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

I honestly do not know whether this will work in all scenarios but I believe this is something that can help.

EDIT: Looks like it kinda works :)

Tried with my country and seems to work perfectly.

http://maps.googleapis.com/maps/api/geocode/json?latlng=6.822716,79.878159&sensor=false

You will get

{
  "status": "ZERO_RESULTS",
  "results": [ ]
}

for example, if the user is in the middle of an ocean by any chance :P

Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • Yeah it works up here for me in Canada, too. Looks like I only need to short_name under the "types": [ "country", "political" ] element in the response. How reliable is it? Does it like throwing 404s? – Rob Szumlakowski Oct 28 '10 at 15:51
  • Not to sound rude but read my lips... Its *Google* Maps :D However, you should know that there are limits to how many requests you can make per day. Check this link http://code.google.com/apis/maps/documentation/geocoding/#Limits – Ranhiru Jude Cooray Oct 28 '10 at 15:53
  • Hey, it's the Google Geocoder class in Android that's flaky. Just because it's from Google doesn't mean it's golden :) – Rob Szumlakowski Oct 28 '10 at 16:04
  • To be honest, I have no idea how this would perform on Android :( The speed of the Internet connection would be a contributing factor. And I really don't know the specifics, but I do not think you need any Geocoder class (if it is already there) for this to work. For all i know, this is simply calling a URL and retrieving the JSON value returned by it. Must be very easy to do in Java. – Ranhiru Jude Cooray Oct 28 '10 at 16:07
  • It certainly is. The usage limits prevent me from using it in this production application, though. Sad :( – Rob Szumlakowski Oct 28 '10 at 18:17
0

What about using an IP-based geolocation service? I would guess that due to regulatory differences network divisions are usually fairly country specific, except in cases such as corporate VPN's where you are virtually plugged into the net somewhere else?

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
0

Try using a web service, like Chris suggested. Here's a list of a few that will do the trick:

http://free-web-services.com/web-services/geo/ip-to-location/

I'm using http://www.hostip.info, which is nice and simple.

Ben H
  • 3,855
  • 1
  • 26
  • 33