Abbes
Abbes

Reputation: 21

Android Getting current location failed

with this function I get longitude = 0 and latitude = 0 I don't know where the problem is

private void getCurrentLocation() {

    Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    if (location != null) {
        //Getting longitude and latitude
        longitude = location.getLongitude();
        latitude = location.getLatitude();

        //moving the map to location
        moveMap();
    }
}

Upvotes: 0

Views: 163

Answers (1)

jgm
jgm

Reputation: 1270

Do this in the onConnected() callback provided by Google API Client, which is called when the client is ready. The following code snippet illustrates the request and a simple handling of the response:

public class MainActivity extends ActionBarActivity implements
    ConnectionCallbacks, OnConnectionFailedListener {
...
@Override
public void onConnected(Bundle connectionHint) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
        mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
    }
}
}

Upvotes: 1

Related Questions