Get current country in Android - traveling

14,741

Solution 1

For #2- its a country code. This is an ISO standard. It will be the 2 letter code. The list can be found here http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2

Solution 2

New answer as promised to fersarr a new solution, this works with the GPS and google maps API, also not my best code but

private GeoPoint getLocation() {
try {
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria,true);
//In order to make sure the device is getting the location, request updates.
// this requests updates every hour or if the user moves 1 kilometer
Location curLocation = locationManager.getLastKnownLocation(provider);
GeoPoint curGeoPoint = new GeoPoint((int)(curLocation.getLatitude() * 1e6), (int)(curLocation.getLongitude()*1e6));
return curGeoPoint;
} catch (NullPointerException e) {
    Log.e("your app name here","Log your error here");
}
return null;
}


public static String getCountryName(Context context, GeoPoint curLocal) {
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
try {
    addresses = geocoder.getFromLocation((double)curLocal.getLatitudeE6() / 1e6,(double)curLocal.getLongitudeE6()/ 1e6,1);
} catch (IOException ignored) {
    Address result;
    if (addresses != null && !addresses.isEmpty()) {
        return addresses.get(0).getCountryName();
    }
    return null;
}
return addresses.get(0).getCountryCode();
}

You will need to do some clean up in this code, as this is on the fly no clean up going on code. but what this does, is first method getLocation will pull in your current Geo point from the GPS (if you can not use a GPS system on the device this will not work) then you can pass the geopoint and the context (your activity) to the getCountryName that should return the country name (in my case it was also US) it returns a list you might have to deal with more then on country code coming back for disputed locations.

Share:
14,741
fersarr
Author by

fersarr

Updated on June 04, 2022

Comments

  • fersarr
    fersarr almost 2 years

    I'm making an app in which I assign different values to each country and do something based on that value. Like:

    Argentina 3
    Australia 7
    USA 23
    

    To choose the country, I need to use the user's current country, for that I'm using Google's geocoder:

    geocoder.getFromLocation
    

    Geocoder documentation

    But if I have many users (hopefully), it will be a problem, because of the API usage restriction of 2500 (With/Without an API key: 2,500 requests per 24 hour period.)
    Geocoder API Limits

    Question 1: About the usage restriction, Is that number the maximum amount of requests for all the users using my map?

    EDIT: I found this

    As geocoding limits are per user session, there is no risk that your application will reach a global limit as your userbase grows. Client-side geocoding will not face a quota limit unless you perform a batch of geocoding requests within a user session. Therefore, running client-side geocoding, you generally don't have to worry about your quota. usage Limits

    Question 2: Let's say I do use Geocoder.getFromLocation(), Is there a list of the country names that Google uses? For example, they could have "USA", "US" or "United States", or even "The United States of America". I need this is in order to access the country's value in the table above.

    I'm thinking about initially obtaining the country using the typical ways:

    telephonyManager.getSimCountryIso();
    

    or

    context.getResources().getConfiguration().locale.getCountry(); 
    

    and adding a refresh button so the user can update it when traveling (using geocoder).

    Question 3: Do you have any other suggestions? alternatives?

  • mpop
    mpop about 10 years
    Ok maybe not, but some of the other answers on the link provided above does try that such as ---- You could use getNetworkCountryIso() from TelephonyManager to get the country the phone is currently in (although apparently this is unreliable on CDMA networks). --- TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE‌​); String countryCode = tm.getSimCountryIso(); ---- both of those might work, again see the first link I provided.
  • fersarr
    fersarr about 10 years
    yea, thanks. I saw this post too but to avoid those CDMA problematic cases, i think it could be safer to use geocoder, do you know about the API usage restrictions?
  • mpop
    mpop about 10 years
    Let me test some code out first, if it works I will post a new answer give me a few please.