HeatKai C HeatKai C - 1 year ago 73
Android Question

Location(Longitude, Latitude) returns 0

I'm trying to take current location's longitude and latitude and keep it in the google firebase database. The source code works fine but the longitude and latitude saved as 0.0:

this is how it looks like in the database.

How to fix this and get the correct current location?

Don't mind the dozens of import classes, I didn't remove anything yet.

I am really sorry if this is a duplicate question.

public class Register extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
EditText edtemail, edtpass, edtnumber;
Button btnRegister, btnLogin, forgotpwdbtn;
private FirebaseAuth auth;
private DatabaseReference mDatabase;

private GoogleApiClient googleApiClient;
private Location location;
private double longitude;
private double latitude;
String mUserId;

protected void onCreate(Bundle savedInstanceState) {

btnRegister = (Button) findViewById(R.id.btnRegister);
btnLogin = (Button) findViewById(R.id.btnLogin);
forgotpwdbtn = (Button) findViewById(R.id.forgotpwdbtn);

auth = FirebaseAuth.getInstance();

mDatabase = FirebaseDatabase.getInstance().getReference();
mUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();

edtemail = (EditText) findViewById(R.id.edtemail);
edtpass = (EditText) findViewById(R.id.edtpass);
edtnumber = (EditText) findViewById(R.id.edtnumber);


public void onClick(View view) {

switch (view.getId()) {

case R.id.forgotpwdbtn:
Intent passwordIntent = new Intent(Register.this, ForgotPasswordActivity.class);
case R.id.btnLogin:
Intent intent = new Intent(Register.this, LoginActivity.class);
case R.id.btnRegister:
googleApiClient = new GoogleApiClient.Builder(this)

final String key = mDatabase.child("data").push().getKey();
final String email = edtemail.getText().toString().trim();
final String password = edtpass.getText().toString().trim();
final String number = edtnumber.getText().toString().trim();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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.
location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (location != null) {
//Getting longitude and latitude
longitude = location.getLongitude();
latitude = location.getLatitude();

String lon = String.valueOf(longitude);
String lat = String.valueOf(latitude);

Item newItem = new Item(email, password, number, lon, lat);
Map<String, Object> itemValues = newItem.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/data/" + mUserId + "/" + key, itemValues);

auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(Register.this, new OnCompleteListener<AuthResult>() {

public void onComplete(@NonNull Task<AuthResult> task) {

if (task.isSuccessful()) {

Toast.makeText(Register.this, "Registration success", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Register.this, LoginActivity.class);

} else {
Toast.makeText(Register.this, "Registration failed." + task.getException(), Toast.LENGTH_SHORT).show();

public void onConnected(@Nullable Bundle bundle) {


public void onConnectionSuspended(int i) {


public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {


Answer Source

You are not connecting to GoogleApiClient. Create GoogleApiClient object in onCreate method. Override two more methods, onStart and onStop. In onStart method, connect to GoogleApiClient by calling googleapiclient.connect(). And in onStop method, disconnect the client by calling if(googleapiclient.isConnected()) { googleapiclient.disconnect() }.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download