Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're saying that you need GPS location first if its available, but what you did is first you're getting location from network provider and then from GPS. This will get location from Network and GPS as well if both are available. What you can do is, write these cases in <strong><code>if..else if</code></strong> block. Similar to-</p> <pre><code>if( !isGPSEnabled &amp;&amp; !isNetworkEnabled) { // Can't get location by any way } else { if(isGPSEnabled) { // get location from GPS } else if(isNetworkEnabled) { // get location from Network Provider } } </code></pre> <p>So this will fetch location from GPS first (if available), else it will try to fetch location from Network Provider.</p> <p><strong>EDIT</strong>:</p> <p>To make it better, I'll post a snippet. Consider it is in <strong><code>try-catch</code></strong>:</p> <pre><code>boolean gps_enabled = false; boolean network_enabled = false; LocationManager lm = (LocationManager) mCtx .getSystemService(Context.LOCATION_SERVICE); gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); Location net_loc = null, gps_loc = null, finalLoc = null; if (gps_enabled) gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (network_enabled) net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (gps_loc != null &amp;&amp; net_loc != null) { //smaller the number more accurate result will if (gps_loc.getAccuracy() &gt; net_loc.getAccuracy()) finalLoc = net_loc; else finalLoc = gps_loc; // I used this just to get an idea (if both avail, its upto you which you want to take as I've taken location with more accuracy) } else { if (gps_loc != null) { finalLoc = gps_loc; } else if (net_loc != null) { finalLoc = net_loc; } } </code></pre> <p>Now you check <code>finalLoc</code> for <code>null</code>, if not then <code>return</code> it. You can write above code in a function which returns the desired (<code>finalLoc</code>) location. I think this might help.</p>
 

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