2

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?

fersarr
  • 3,399
  • 3
  • 28
  • 35
  • possible duplicate of [Where am I? - Get country](http://stackoverflow.com/questions/3659809/where-am-i-get-country) – mpop Feb 17 '14 at 07:52

3 Answers3

3

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

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

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.

mpop
  • 499
  • 11
  • 21
0

I did a quick Google search and found Where am I? - Get country

basically if you call

String locale = getResources().getConfiguration().locale.getCountry();
Log.d("IssuesEtc","the local is " + locale);

it should give you the country code. I ran the above snipit, and I got back the local is US so it appears to work on my phone (Samsung S blaze, cyagin mod, on T-Mobile) I should also note the the call was done inside of a class that extended Activity, if you are out side of a class that has in it ansestory context you will need to obtain the context to run the getResources() method.

Community
  • 1
  • 1
mpop
  • 499
  • 11
  • 21
  • 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. – mpop Feb 17 '14 at 07:49
  • 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? – fersarr Feb 17 '14 at 07:57
  • Let me test some code out first, if it works I will post a new answer give me a few please. – mpop Feb 17 '14 at 07:59