Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Country is the User Currently In?

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

like image 668
Rob Szumlakowski Avatar asked Nov 06 '22 07:11

Rob Szumlakowski


1 Answers

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

like image 87
Ranhiru Jude Cooray Avatar answered Nov 15 '22 10:11

Ranhiru Jude Cooray