Finding your current location works when using Wi-Fi, but it fails when using GPS

I developed an application to search for a user's location using GPS.

There is no mistake. It finds the location using Wifi correctly, but in GPS mode it does not return any values. I added the required resolution to the manifest, and GPS is on.

Can someone tell me how to get location using GPS?

I have included my code here:

package cgaloation; public class UseGps extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener mlocListener = new MyLocationListener(); mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); } /* Class My Location Listener */ public class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { Geocoder gp = new Geocoder(getBaseContext()); try { ArrayList<Address> address = (ArrayList<Address>) gp .getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); if (address != null && address.size() > 0) { Toast.makeText(getBaseContext(), address.get(0).toString(), Toast.LENGTH_LONG).show(); } } catch (IOException e) { e.printStackTrace(); } } @Override public void onProviderDisabled(String provider) {} @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) {} @Override public void onProviderEnabled(String arg0) {} } public void onProviderEnabled(String provider) { // Toast.makeText( // getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); } public void onStatusChanged(String provider, int status, Bundle extras) {} } 
+4
source share
1 answer

I have two thoughts ...

  • Your code does not check if the GPS provider is turned on. I see that the commented code sends Toast when ANY provider is enabled, but you can check if the GPS provider is enabled in onCreate ().

  • How long do you want to get a GPS location? GPS_PROVIDER it may sometimes take a while to get a GPS fix. I saw the TTFF values โ€‹โ€‹(fist fixation time) two minutes before. To make matters worse, I saw dramatic TTFF differences between different versions of Android as well as different devices.

I would also recommend that you try getting a GPS fix from another application to see how long it takes. I usually launch either Google Maps or GPS status (free from the market) to understand how long they take the GPS solution. If you are trying to use Google Maps, make sure that it is receiving a GPS fix, not just the location from NETWORK_PROVIDER (the GPS icon at the top of the screen should stop blinking).

0
source

Source: https://habr.com/ru/post/1341392/


All Articles