Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: getting location using GPS provider returns a wrong location
    primarykey
    data
    text
    <p>I'm implementing an app that gets your location when you open it. It only gets it when the activity is created, so I don't need a <code>LocationListener</code>. I'm testing it in two real devices.</p> <p>First I try to get the location using <code>GPS_PROVIDER</code>. If the result is NULL, I use <code>NETWORK_PROVIDER</code>. Since I'm testing it inside a building is to be expected that I won't be able to get the location using the GPS provider.</p> <p>In one of the devices the location using <code>GPS_PROVIDER</code> is NULL, and the <code>NETWORK_PROVIDER</code> returns the correct location. But in the other device the location using GPS_PROVIDER is not NULL, but it's wrong (it's in a different city than the one I'm currently am!). This is the code I'm using:</p> <pre><code>private void getPosition() { locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); myloc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if(myloc == null) myloc = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if(myloc != null){ Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); try{ List&lt;Address&gt; addresses = geoCoder.getFromLocation(myloc.getLatitude(), myloc.getLongitude(), 1); if(addresses.size()&gt;0){ String add = ""; for(int i=0; i&lt;addresses.get(0).getMaxAddressLineIndex(); i++) add += addresses.get(0).getAddressLine(i)+"\n"; placeEdit.setText(add); } }catch(IOException e){ e.printStackTrace(); } } } </code></pre> <p>How can I know the location returned by the GPS provider is wrong? Should I look the network provider first and if it's null use the gps? This app is supposed to be used outside, so I'd like to use the gps first since it's more accurate.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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