Use Highest Precision Possible on Android GPS

Asked

Viewed 103 times

0

I’m building an app that by going through a "line", defined by an interval of latitudes and longitudes, recognizes the passage and displays it in a View.

But I noticed that when passing through the gap at a higher speed, on board a vehicle, it does not recognize the passage.

How can I get this accuracy at a higher speed?

package br.com.nobre.ntrack.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import br.com.nobre.ntrack.R;
import br.com.nobre.ntrack.config.Permissoes;

public class NavegacaoActivity extends AppCompatActivity {

    private GoogleMap mMap;
    private String[] permissoes = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
    private LocationListener locationListener;
    private LocationManager locationManager;
    private TextView txtLat, txtLong, txtVelocidade, txtParcial;

    private Double testeMaior;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teste_navegacao);

        Permissoes.validarPermissoes(permissoes, this, 1);

        txtLat = findViewById(R.id.txtLatitude);
        txtLong = findViewById(R.id.txtLongitude);
        txtVelocidade = findViewById(R.id.txtVelocidade);
        txtParcial = findViewById(R.id.txtParcial);

        Double lat1, lat2;
        lat1 = -29.909756; // maior
        lat2 = -29.909811;


        Log.i("teste", "maior = " + testeMaior);
        txtParcial.setText("Não passou");

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.i("teste de localizacao", "Localizacao: " + location.toString());
                Double latitude = location.getLatitude();
                Double longitude = location.getLongitude();
                float velocidade = location.getSpeed();

                txtLat.setText("lat: " + latitude);
                txtLong.setText("long: " + longitude);
                double velocidadeEmKm = (velocidade * 3.6);
                txtVelocidade.setText("velocidade: " + velocidadeEmKm);

                //-29.909737, -51.142835
                //-29.909846, -51.142851

                if (latitude >= -29.909846 && latitude <= -29.909737) {
                    if (longitude >= -51.142851 && longitude <= -51.142835) {
                        // esta passando na parcial teste efetuado com sucesso


                        txtParcial.setText("Passou pela parcial");
                    }
                }
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };

        // recuperar localização do usuario
        /*
         * 1 - provedor de localização
         * 2 - Tempo mínimo entre atualizações de localização (milesegundos)
         * 3 - Distância mínima entre atualizações de localização (metros)
         * Location Listner ( Para recebermos as atualizações)*/
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        for (int permissaoResultado : grantResults) {
            if (permissaoResultado == PackageManager.PERMISSION_DENIED) {
                //Alerta
                alertaValidacaoPermissao();
            } else if (permissaoResultado == PackageManager.PERMISSION_GRANTED) {
                // recuperar localização do usuario
                /*
                 * 1 - provedor de localização
                 * 2 - Tempo mínimo entre atualizações de localização (milesegundos)
                 * 3 - Distância mínima entre atualizações de localização (metros)
                 * Location Listner ( Para recebermos as atualizações)*/
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                }
            }
        }
    }

    private void alertaValidacaoPermissao() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getResources().getString(R.string.permissoes_negadas));
        builder.setMessage(getResources().getString(R.string.mensagem_permissoes_negadas));
        builder.setCancelable(false);
        builder.setPositiveButton(getResources().getString(R.string.confirmar_permissoes_negadas), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });

        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        locationManager.removeUpdates(locationListener);
    }
}
  • I don’t know how long this interval is that you mentioned and I also don’t know how long your application reads the coordinate. But let’s say that it’s 100 meters wide and for as long as you’ve been between the coordinates the system hasn’t read or if the only reading it’s made has dropped out of that range it won’t recognize.

  • When you are driving with Waze and out on a parallel track, it takes a long time to 'realize' that you have left the main road.

  • Another important detail Waze ( for example ) achieves a good level of accuracy because it uses another map to correct the points it receives from GPS. If you do not make any correction the points can be well spread, and it is natural that.

1 answer

0


Use Fusedlocationproviderclient and set the priority LocationRequest as PRIORITY_HIGH_ACCURACY

See the details on accuracy in this link

In short, the Google Play Services API has the intelligence to get an accurate location by merging GPS_PROVIDER + NETWORK_PROVIDER + PASSIVE_PROVIDER.

Browser other questions tagged

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