I am trying to get the location of a user through LocationManager.NETWORK_PROVIDER but it always returns a blank string. Only sometimes I get the valid data. Is there any way to make this method re-iterate until it gives me a location? I would think that location would come quickly and there would be no need for this method. I have tried re-calling it until it doesn't display a blank string, but nothing comes up.
Here is the method I call
public String getLocation(Locale locale, Context context, boolean city, boolean postal, boolean state_prov) throws IOException{
LocationManager locMan = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locList = new MyLocList();
Geocoder gC = new Geocoder(context,locale);
Location gpsLocation = locMan.getLastKnownLocation(locMan.GPS_PROVIDER);
locMan.requestLocationUpdates(locMan.NETWORK_PROVIDER, 500, 200, locList);
Location networkLocation = locMan.getLastKnownLocation(locMan.NETWORK_PROVIDER);
if (city)
return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getSubAdminArea());
else if (postal)
return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getPostalCode());
else if (state_prov)
return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getAdminArea());
else
return "Nothing";
}
I usually ask for the city or postal code, but it most of the time it comes up empty. Using the
Location gpsLocation = locMan.getLastKnownLocation(locMan.GPS_PROVIDER);
results in a NullPointer, so I am using the NETWORK_PROVIDER
I have set these permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
And this is what I call it with
String data = getLocation(Locale.getDefault(),getBaseContext(),true,false,false);
I keep on getting a blank string as the result.
I asked a question before about not receiving the city, but now it seems that no data is coming, and if it is, its not consistently available.