Note that there are some explanatory texts on larger screens.

plurals
  1. POLocation not always appearing
    primarykey
    data
    text
    <p>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.</p> <p>Here is the method I call</p> <pre><code> 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"; } </code></pre> <p>I usually ask for the city or postal code, but it most of the time it comes up empty. Using the </p> <pre><code> Location gpsLocation = locMan.getLastKnownLocation(locMan.GPS_PROVIDER); </code></pre> <p>results in a NullPointer, so I am using the NETWORK_PROVIDER</p> <p>I have set these permissions:</p> <pre><code> &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"&gt;&lt;/uses-permission&gt; &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"&gt;&lt;/uses-permission&gt; </code></pre> <p>And this is what I call it with</p> <pre><code> String data = getLocation(Locale.getDefault(),getBaseContext(),true,false,false); </code></pre> <p>I keep on getting a blank string as the result. </p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload