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) {
}
}