Return an address

0

I'm finalizing a project where it will open an alertDialog. As soon as the user presses OK they will make a confirmation and their location will be informed by a toast. I would like to know how I can return the place where the user is.

    
asked by anonymous 10.07.2018 / 01:24

1 answer

1

Make it happen:

public class main extends AppCompatActivity {

    LocationListener locationListener;
    LocationManager locationManager;

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

        //Faço o objeto da classe
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationListener = new MyLocationListener();

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        assert locationManager != null;
        //Requesto a localização, os parâmetros 500 e 0 são intervalo que irei pegar o localização e a variação em metros que irei pegar respectivamente
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, locationListener);

    }

    public class MyLocationListener implements LocationListener {

            @Override
            public void onLocationChanged(Location loc) {

                   //Peguei a latitude e longitude
                   double lat = loc.getLatitude();
                   double log = loc.getLongitude();

                   //Agora transformo em endereço:
                   String cityName = null;
                   Geocoder gcd = new Geocoder(context, Locale.getDefault());
                   List<Address> addresses;
                   try {
                        addresses = gcd.getFromLocation(loc.getLatitude(),
                        loc.getLongitude(), 1);

                        if (addresses.size() > 0) {

                           //Aqui você chama seu alert, o endereço se pega da maneira abaixo:
                           //Endereço = addresses.get(0).getLocality());

                           System.out.println(addresses.get(0).getLocality());
                           cityName = addresses.get(0).getLocality();

                        }
                    }catch (IOException e) {
                           e.printStackTrace();
                    }

                   //Depois de pegar a localização eu para o onLocationChanged
                   locationManager.removeUpdates(this);

            }

            @Override
            public void onProviderDisabled(String provider) {}

            @Override
            public void onProviderEnabled(String provider) {}

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

        }
}

More chewed than this only if I do your alertDialog kkkkkkkkkk, remember to make a method that checks if GPS is active before attempting to pick up the location, as an exception may occur if it is turned off.

Remembering what you should add in Manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    
10.07.2018 / 01:49