2

I am going in circles to just get the current location in android SDk ..

i used this repository from android training site https://github.com/googlesamples/android-play-location/tree/master/BasicLocationSample

but i receive no location found error when i start app on emulator i used the DDMS send, and i went to settings on emulator and the location settings is on . .. what have i missed ??

Update: The code :

ublic class MainActivity extends ActionBarActivity implements
    ConnectionCallbacks, OnConnectionFailedListener {

protected static final String TAG = "basic-location-sample";

/**
 * Provides the entry point to Google Play services.
 */
protected GoogleApiClient mGoogleApiClient;

/**
 * Represents a geographical location.
 */
protected Location mLastLocation;

protected TextView mLatitudeText;
protected TextView mLongitudeText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    mLatitudeText = (TextView) findViewById((R.id.latitude_text));
    mLongitudeText = (TextView) findViewById((R.id.longitude_text));

    buildGoogleApiClient();
}

/**
 * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
 */
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
/**
 * Runs when a GoogleApiClient object successfully connects.
 */
@Override
public void onConnected(Bundle connectionHint) {
    // Provides a simple way of getting a device's location and is well suited for
    // applications that do not require a fine-grained location and that do not need location
    // updates. Gets the best and most recent location currently available, which may be null
    // in rare cases when a location is not available.
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
        mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
    } else {
        Toast.makeText(this, R.string.no_location_detected, Toast.LENGTH_LONG).show();
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
    // onConnectionFailed.
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}


@Override
public void onConnectionSuspended(int cause) {
    // The connection to Google Play services was lost for some reason. We call connect() to
    // attempt to re-establish the connection.
    Log.i(TAG, "Connection suspended");
    mGoogleApiClient.connect();
}

public  void getLoc (View view){
    mGoogleApiClient.connect();

}
}
Essam Mahdy
  • 139
  • 1
  • 8
  • please post your code to be more clear – Aakash Aug 14 '15 at 21:06
  • 1
    http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/ : have a look – Aakash Aug 14 '15 at 21:13
  • you can go through this question http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android?rq=1 – Sandeep Londhe Aug 14 '15 at 21:18
  • you are using getLastLocation , If last location is null error will be displayed. Instead you should use location listener to monitor changes in location – Gautam Aug 15 '15 at 04:59
  • this question's first answer worked .. : http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a Thx 4 yor help guys – Essam Mahdy Aug 15 '15 at 11:27

0 Answers0