I am new to android and I have tried to pass 2 double variables from one method to another to upload it to the firebase database, but I have not succeeded, the variables come from a location placed on the map. How could I pass the data from searchLocation to SaveDatasMap and upload it to the database?
I tried to put them static in the methods but it does not let me place it. I tried to place the double variables at the beginning but I can not get them to have the value that they are asked to: have the longitude and latitude in their selected variables
public void searchLocation(View view) {
EditText locationSearch = (EditText) findViewById(R.id.editText);
final String location = locationSearch.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
final Address address = addressList.get(0);
final LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(latLng).title(location));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
Toast.makeText(getApplicationContext(),address.getLatitude()+" "+address.getLongitude(),Toast.LENGTH_LONG).show();
double longitud = address.getLatitude();
double latitud = address.getLatitude();
}
}
public void SaveDatasMap (View view)
{
GuardarUbicacion = (Button) findViewById(R.id.guardarubicacion);
final DatabaseReference RootRef;
RootRef = FirebaseDatabase.getInstance().getReference();
RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!(dataSnapshot.child("Empresa").child(currentUserID).exists()))
{
HashMap<String, Object> userdataMap = new HashMap<>();
userdataMap.put("Longitud", longitud);
userdataMap.put("Longitud", latitud);
RootRef.child("Empresa").child(currentUserID).updateChildren(userdataMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
Toast.makeText(MapTemporalActivity.this, "Se ha guardado la posicion exitosamente", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MapTemporalActivity.this, LoginActivity.class);
startActivity(intent);
}
else
{
Toast.makeText(MapTemporalActivity.this, "Hubo un error", Toast.LENGTH_SHORT).show();
}
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
I need to pass the variables longitude and latitude. I did the data upload to the database but I don't know how to pass the searchLocation's double variables or pass directly address.getLatitude () and address.getLongitude() to the method SavesDataMap
I understand that I would have to do the method in static but it does not allow me android studio
You can make these two variable global. instantiate them like this. double longitud; double latitud;
In your searchLocation method do the following: longitud = address.getLatitude(); latitud = address.getLatitude();
longitud = address.getLatitude(); latitud = address.getLatitude();
Then in the SaveDatasMap method you should be able to get the data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.