I am trying to Reverse GeoCode current Lat/Long data to just Admin Area and Sub Admin Area using the Geocoder class. I am using the NETWORK_PROVIDER as the Location provider. Here is the code that I use and it works. The problem is sometimes it does not give me any location sometimes it does.
Android Manifest permissions I am using.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
Here is the rest of the code that does give me correct location from time to time but nothing at other times.
double latPoint = 0;
double lngPoint = 0;
List<Address> myList = null;
getApplicationContext();
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location recentLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (recentLoc != null){
latPoint = recentLoc.getLatitude();
lngPoint = recentLoc.getLongitude();
Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
if (myLocation != null){
try {
myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (myList.size()> 0){
String AdminArea = myList.get(0).getAdminArea();
String SubAdminArea = myList.get(0).getSubAdminArea();
String Zipcode = myList.get(0).getPostalCode();
String urlocation = SubAdminArea + ", " + AdminArea + " " + Zipcode;
Context context1 = getApplicationContext();
int duration1 = Toast.LENGTH_LONG;
CharSequence text1 = urlocation;
Toast toast1 = Toast.makeText(context1, text1, duration1);
toast1.show();
}
}
Are there restrictions with the network providers how frequently you can request location data?
How can I make sure that everytime its run this can give me valid location data.
Thanks in advance for any insight into this.
PS: I have run this code on my real device running Froyo 2.2 (Verizon Wireless). Target API Level 8.
This is what I got from my debugging.
Latitude Longitude is always fetched but the getFromLocation often returns null for the latitude longitude data passed to it. So what should be the workaround? May be pass the Lat/Long info to Google Maps API for reverse geocoding. Will be glad to receive any input.