Recall variables from the onConnected method

2

I have a problem with some variables that I want to recover from an onConnected method, these variables contain the current latitude and longitude of my device and I want to recover them to store in a global variable and be able to use those global variables in the onMapReady method ( ).

This is my onConnected () method, the two variables I want to convert into global are txtLatitud and txtLength, here in this method they already have a value that is assigned by retrieving my current coordinates;

@Override
    public void onConnected(@Nullable Bundle bundle) {

        int leer = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);
        if (leer == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, PERMISOS, REQUEST_CODE);
        }

        ultimaPosicion = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

        if (ultimaPosicion != null){
            latitudActual = String.valueOf(ultimaPosicion.getLatitude());
            longitudActual = String.valueOf(ultimaPosicion.getLongitude());

            txtLatitud.setText(latitudActual);
            txtLongitud.setText(longitudActual);

        }

    }

And this is my onMapReady () method, the variables that I want to retrieve as global are to be used in that line of code:

  

final LatLng example = new LatLng (19.4188761, -99.1552811)

The problem is that I can not recover them as global, I tried to use this.test = txtLatidud , but the value that returns to me is null

 @Override
    public void onMapReady(GoogleMap googleMap) {

        final LatLng ejemplo = new LatLng(19.4188761, -99.1552811);




        Toast.makeText(this,"Latitud: " + prueba, Toast.LENGTH_LONG).show();

        googleMap.addMarker(new MarkerOptions()
                .position(ejemplo)
                .title("Prueba")
                .snippet("Población: 2,965 millones")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
//                .alpha(.5f)
//                .flat(true)
                .draggable(true));

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ejemplo, 16));


    }

Complete code:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback {

    private static final String[] PERMISOS = {
            Manifest.permission.ACCESS_FINE_LOCATION
    };

    private static int REQUEST_CODE = 1;
    private GoogleApiClient googleApiClient;
    private Location ultimaUbicacion;
    Double latitudObtenida;
    Double longitudObtenida;
    private TextView txtLat;
    private TextView txtLon;

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

         txtLat = (TextView)findViewById(R.id.txtLat);
         txtLon = (TextView)findViewById(R.id.txtLon);


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

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);



    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        int leer = ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION);
        if (leer == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, PERMISOS, REQUEST_CODE);
        }


        final LatLng BUENOS_AIRES = new LatLng(-34.637936, -58.406372);

        googleMap.addMarker(new MarkerOptions()
                .position(BUENOS_AIRES)
                .title("Prueba")
                .snippet("Población: 2,965 millones")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
//                .alpha(.5f)
//                .flat(true)
                .draggable(true));

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitudObtenida, longitudObtenida), 2.0f));

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {

        int leer = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);
        if (leer == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, PERMISOS, REQUEST_CODE);
        }

        ultimaUbicacion = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

        if (ultimaUbicacion != null){
            latitudObtenida = ultimaUbicacion.getLatitude();
            longitudObtenida = ultimaUbicacion.getLongitude();
        }

    }

    @Override
    protected void onStop() {
        googleApiClient.disconnect();
        super.onStop();
    }

    @Override
    protected void onStart() {
        googleApiClient.connect();
        super.onStart();
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }
}
    
asked by Enrique Espinosa 27.06.2018 в 16:59
source

1 answer

0

The variables txtLatitud and txtLongitud refer to TextView 's, are not the values of latitude and longitude that you need, the values you need are:

latitudActual and longitudActual

these will have value when you call the onConnected() method and are the ones you would use here:

 @Override
    public void onMapReady(GoogleMap googleMap) {

        final LatLng ejemplo = new LatLng(latitudActual, longitudActual);
    ...
    ...

If the onConnected() method subsequently obtains the onMapReady() call from the latitude and longitude data then you can call the moveCamera() method to update the position on the map.

It is important to convert values to Double by Double.valueOf() :

 googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.valueOf(latitudActual), Double.valueOf(longitudActual)), 2.0f));
    
answered by 28.06.2018 в 00:01