How to take latitude and longitd with android button

1

Good afternoon guys, I am implementing a process that must take the geographical coordinates when clicking a button, I currently have this code to take the coordinates, but it does not work for me: (

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

import com.example.pedromendieta.ipal.MainActivity;
import com.example.pedromendieta.ipal.ValidarCumplimiento;

public class MyLocationListener implements LocationListener {

    private ValidarCumplimiento padre;

    public ValidarCumplimiento getMainActivity() {
        return padre;
    }

    public void setMainActivity(ValidarCumplimiento padre) {
        this.padre = padre;
    }

    @Override
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc.getLongitude();
        this.padre.setLocation(loc);
    }

    @Override
    public void onProviderDisabled(String provider) {
        // Este mŽtodo se ejecuta cuando el GPS es desactivado
//        "GPS Desactivado";
    }

    @Override
    public void onProviderEnabled(String provider) {
        // Este mŽtodo se ejecuta cuando el GPS es activado
//        messageTextView.setText("GPS Activado");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // Este mŽtodo se ejecuta cada vez que se detecta un cambio en el
        // status del proveedor de localizaci—n (GPS)
        // Los diferentes Status son:
        // OUT_OF_SERVICE -> Si el proveedor esta fuera de servicio
        // TEMPORARILY_UNAVAILABLE -> Temp˜ralmente no disponible pero se
        // espera que este disponible en breve
        // AVAILABLE -> Disponible
    }
}

in the class validate compliance in the Oncreate I have this:

LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        MyLocationListener mlocListener = new MyLocationListener();
        mlocListener.setMainActivity(this);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) mlocListener);

and finally this is the setLocation () method called in the MyLocationListener class in the onLocationChanged event:

public void setLocation(Location loc) {
        //Obtener la direcci—n de la calle a partir de la latitud y la longitud
        if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
            try {
                Geocoder geocoder = new Geocoder(this, Locale.getDefault());
                List<Address> list = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
                lat = loc.getLatitude() + "";
                lng = loc.getLongitude() + "";
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

I hope you can get me out of trouble, thank you very much

    
asked by Felipe Mendieta Perez 22.02.2017 в 20:35
source

1 answer

1

Try changing the parameters you send to the requestLocationUpdates () method; like this:

  mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60, 5, (LocationListener) mlocListener);

The first parameter is the provider that you will use in this case the GPS

The second parameter is the time you request the updates in milliseconds, so 1000 * 60 is equal to one minute (you have it at zero, therefore, you are not requesting updates)

The third parameter is the minimum distance between updates in meters (You have it at zero therefore you are not requesting updates).

    
answered by 23.02.2017 в 04:49