Reputation:
I'm trying to get the country name using NETWORK_PROVIDER, i wrote a method
private int getCurrentLocation(Location location) throws IOException {
Geocoder geocoder = new Geocoder(this, getResources().getConfiguration().locale);
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);
String CurrentCountryFullName = addresses.get(0).getCountryName();
if (CurrentCountryFullName != null) {
for (StandartCurrency standartCurrency: RecourcesForStandartCurrencies.getStandartCurrencyList()){
if (RecourcesForStandartCurrencies.getCountryFullName(standartCurrency.getPosition()).toLowerCase().contains(CurrentCountryFullName.toLowerCase()))
return standartCurrency.getPosition();
}
}
return AddedCurrency.getAddedCurrenciesCount();
}
I call this method from
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
leftFieldIndex = getCurrentLocation(locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER));
} catch (IOException e) {
e.printStackTrace();
My AdnroidManifest.xml contains
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
I'm testing on Nexus 4, it has wifi connection with working internet, but still not working.
Upvotes: 5
Views: 6249
Reputation: 3472
I had the same problem and I switched to v3 web API. Now I'm using the java client and all work well.
All info are in the Google Maps page
Upvotes: 0
Reputation: 7663
Geocoder
is not stable and sometimes stops working. You have not posted an error log or exception but from experience, when Geocoder
breaks it simply returns an empty list. Unfortunately there is no way to check if Geocoder
is working in advance (there is only a method to check if Geocoder
is available, which will return true even if it is not functioning)
The work around is to connect to the Google Maps web api when your Geocoder
returns an empty list. Look at this post for an example of the code. You're pretty much going to just check to see if the list returned from the Geocoder
is null or empty first and if it is use an AsyncTask
to make a web api call to the Google Maps api. The end result is the same so you should not notice a difference in functionality.
private int getCurrentLocation(Location location) throws IOException {
Geocoder geocoder = new Geocoder(this, getResources().getConfiguration().locale);
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);
if(addresses != null && addresses.size() > 0 {
//use addresses
} else {
//call web API async task
}
}
Depending on what you're doing with the addresses, it may make sense to break out your Geocoder
implementation into a separate class. That way you can use an Interface
to connect your Geocoder
class to your activity or fragment and make a seamless transition between your AsyncTask
web api call and Geocoder
should the later stop working (you obviously cannot return the results of the AsyncTask
in the same method because it is not delivered at the time the method is finished)
Upvotes: 7