'How to get the latitude and longitude to Oncreate method

i am storing address details to firestore. now im using mapview in this activity. and its getting current location. now i want to pass the longitude and latitude to firebase , then i need to get the values to oncreate method.

i am using Address_location variable , i want to get latitude pass to that

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

    mName = findViewById(R.id.ad_name);
    mCity = findViewById(R.id.ad_city);
    mAddress = findViewById(R.id.ad_address);
    mCode = findViewById(R.id.ad_code);
    mNumber = findViewById(R.id.ad_phone);
    mAddAddressbtn = findViewById(R.id.ad_add_address);

    mStore = FirebaseFirestore.getInstance();
    mAuth = FirebaseAuth.getInstance();

    //setSupportActionBar(mToolbar);
    //getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //TESt
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    fetchLastLocation();


    mAddAddressbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = mName.getText().toString();
            String city = mCity.getText().toString();
            String address = mAddress.getText().toString();
            String code = mCode.getText().toString();
            String number = mNumber.getText().toString();
            String final_address = "";
            if (!name.isEmpty()) {
                final_address += name + ", ";
            }
            if (!city.isEmpty()) {
                final_address += city + ", ";
            }
            if (!address.isEmpty()) {
                final_address += address + ", ";
            }
            if (!code.isEmpty()) {
                final_address += code + ", ";
            }
            if (!number.isEmpty()) {
                final_address += number + ", ";
            }

            Map<String, String> mMap = new HashMap<>();
            mMap.put("address", final_address);
            mMap.put("name", name);
            mMap.put("Address_Lane", address);
            mMap.put("city", city);
            mMap.put("Postal_code", code);
            mMap.put("Phone_number", number);


            mStore.collection("User").document(mAuth.getCurrentUser().getUid())
                    .collection("Address").add(mMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                @Override
                public void onComplete(@NonNull Task<DocumentReference> task) {
                    if (task.isSuccessful()) {
                        Toast.makeText(AddAddressActivity.this, "Address Added", Toast.LENGTH_SHORT).show();
                        finish();
                    }

                }
            });


           


        }
    });


**i want the latitude value and longitude here**
    Toast.makeText(AddAddressActivity.this,Address_location, Toast.LENGTH_SHORT).show();

}

private void fetchLastLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
       ActivityCompat.requestPermissions(this,new String[]
               {Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
        return;
    }
    Task<Location> task = fusedLocationProviderClient.getLastLocation();
    task.addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            if (location != null){
                currentLocation = location;
                //Toast.makeText(getApplicationContext(), currentLocation.getLatitude()
                //       +""+currentLocation.getLongitude(), Toast.LENGTH_SHORT).show();

                String Address_location = Double.toString(currentLocation.getLatitude());


                SupportMapFragment supportMapFragment = (SupportMapFragment)
                        getSupportFragmentManager().findFragmentById(R.id.map);
                supportMapFragment.getMapAsync(AddAddressActivity.this);
            }
        }
    });

}



@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
    LatLng latLng = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions().position(latLng)
            .title("I am Here.");
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,5));
    googleMap.addMarker(markerOptions);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                fetchLastLocation();

            }
            break;
    }

}

}



Solution 1:[1]

If I understood your use case properly, you should be using member variable for lat & long, until its null don't update details just yet. if you want to update anyway even if onsuccess method is not yet called, start a thread that waits until that variable is not null and then call update to firebase method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Achal Urankar