Very large delay when getting GPS coordinates

Asked

Viewed 165 times

0

I’m having delay problems getting GPS coordinates. I pick them up the moment the user presses a textbox to validate the data. Motorola’s Vics are doing fine. Now testing on the Oneplus 3T (A3000) and on the LG, the delay is between 12 to 15 seconds, no matter where you are or where you are (garage, underground, in a building, etc.). And no matter how many times the user answers the same field, the problem happens.

I read these links in the OS:

Getting Too Much delay from Onlocationchange.?

Why Android Locationmanager has long delay before Location updates start if Setting Accuracy

Location Strategies

My code:

public void getLocation(final LocationListener locationListener) {
        if (locationListener == null) return;
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
        locationRequest.setInterval(500);
        locationRequest.setFastestInterval(500);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest)
                .setAlwaysShow(true);

        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        googleApiClient.connect();
        PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi
                .checkLocationSettings(googleApiClient, builder.build());

        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {

            @Override
            public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
                final Status status = locationSettingsResult.getStatus();
                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_COARSE);
                criteria.setSpeedRequired(true);
                criteria.setAltitudeRequired(false);
                criteria.setBearingRequired(false);
                criteria.setCostAllowed(true);
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        LocationManager locationManager =
                                ((LocationManager) getSystemService(Context.LOCATION_SERVICE));

                        showError("NETWORK: "+ locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
                        showError("GPS: "+locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));

                        String provider = locationManager.getBestProvider(criteria, true);

                        if (App.isVersionGreaterThanLollipop()){
                            if (ActivityCompat.checkSelfPermission(
                                    ChecklistActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
                                    == PackageManager.PERMISSION_GRANTED) {
                                Location loc = locationManager.getLastKnownLocation(provider);
                                currentLcoation = loc;
                                showError("BEST PROVIDER: "+provider+" CURRENT LOCATION: " +loc.getLatitude()+" "+loc.getLongitude());
                                locationManager.requestSingleUpdate(
                                        criteria, locationListener, null);
                            }
                            else {
                                requestLocationPermission(locationListener);
                            }
                            ...
                        }
    ...

Not even forcing NETWORK_PROVIDER I get a location quickly.

For those who have faced this type of problem, what is the best approach to get the coordinates in this scenario and quickly? Note that I am using requestSingleUpdate. This influences getting the fixes?

  • You’re mixing two location services: Google’s Locationservices.API and Android’s Locationmanager. Check out this one reply, On it, if you want to get just one location, add setNumUpdates(1) at the mLocationRequest and adjust the other values according to your needs.

  • @From what I understand of your code, it’s just an update (no offense). This update improves the performance of the delay you mentioned?

  • You’re not using Google’s Locationservices.API to get the location, you’re using Android’s Locationmanager. In that answer you find the most current/recommended (date) way to get the location.

  • @ramaral Ok. Using this solution in newer Androids, the app flies. However, in older Moto G1, G2 takes horrors to climb. There’s a reason for this?

  • @ramaral Just one more comment. Excellent solution.

  • @ramaral I believe your solution is not to be used. Have you seen this link? https://developer.android.com/training/location/receive-location-updates.html

  • This part of the documentation is outdated. It still uses the Fused Location Provider which has been discontinued.

  • You’re right. Not even the Google people understand.

  • If you want to keep up to date follow regularly the Android Developers Blog.

  • @ramaral a question, how do you deal with the delays of getting coordinates and not harm the user experience?

  • If the hardware takes too long to get the location, whatever the reason, you can’t fix it. There is only one way to minimize this: to immediately provide the last known location, obtained with getLastLocation() and then use requestLocationUpdates().

  • @ramaral Exactly what I thought. There is no way to do otherwise than wait for the GPS "charge"

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.