无法获得android中的当前位置

时间:2015-08-14 21:03:46

标签: android gps position location

我想进入android SDk中的当前位置..

我在Android培训网站上使用了此存储库 https://github.com/googlesamples/android-play-location/tree/master/BasicLocationSample

但是当我在模拟器上启动应用程序时,我没有收到位置发现错误  我用DDMS发送, 我去了模拟器上的设置,并且位置设置已打开。  ..我错过了什么?

更新:代码:

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();

}
}

0 个答案:

没有答案