Find current location

Asked

Viewed 119 times

0

I am developing an application where I need to remove the current location of the user, the problem is that the code I developed returnsme the last coordinates existing on the device and I only wanted the current ones but not always get only after my position change is that I get current coordinates , any suggestions to get the current location of the user in more practical way? I still tried to force the result with a for to avoid having the last location but it does not solve the problem.

private TextView txtLatitude;
private TextView txtLongitude;
public Button btcoordenadas;



private Location location;
private Location mCurrentLocation ;
private LocationManager locationManager;


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


    txtLatitude = (TextView) findViewById(R.id.txtLatitude);
    txtLongitude = (TextView) findViewById(R.id.txtLongitude);
    btcoordenadas = (Button) findViewById(R.id.btncoordenadas);


    final double[] latitude = {0.0};
    final double[] longitude = {0.0};

    final LocationManager[] locationManager = {(LocationManager) this.getSystemService(Context.LOCATION_SERVICE)};



    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            for(int i=0; i<=10; i++) {
                mCurrentLocation = location;


                updateUI();


                makeUseOfNewLocation(location);
            }
        }

        private void updateUI() {
            txtLatitude.setText(String.valueOf(mCurrentLocation.getLatitude()));
            txtLongitude.setText(String.valueOf(mCurrentLocation.getLongitude()));

        }


        private void makeUseOfNewLocation(Location location) {

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
    };



    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Solicitar permissão ao usuário.


    }
    else {

        btcoordenadas.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                for(int l=0; l<=10; l++) {           switch (v.getId()) {

                    case R.id.btncoordenadas:

                        if (locationManager[0].isProviderEnabled(LocationManager.NETWORK_PROVIDER))  {

                                locationManager[0] = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                                locationManager[0].requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 600, 0, locationListener);

                                location = locationManager[0].getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                               }
                        else {
                            Log.i("Erro", "A internet não se encontra ligada");
                        }

                }

                break;
                }


            }
        });

    }

}

    }

1 answer

0

For this you can use the Fusedlocationproviderapi which is included in the Playservices package. With this Google Api you can quickly get user location, location changes and most importantly control battery usage with Flags PRIORITY_HIGH_ACCURACY , PRIORITY_LOW_POWER,PRIORITY_NO_POWER , information about the flags.
I’m not going to put an example of implementation because I think the links and example of the Google documentation is enough for you to be able to study and adapt to your code. I’m also leaving you all the google code using the all api on Github. Demo Github Google

Browser other questions tagged

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