我想在我的Android项目中简单地检索设备位置,为了做到这一点,我使用了play-services方法:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder( MainSearchActivity.this )
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected( Bundle bundle ){
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if( location == null ){
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
lastLocation = location;
}
});
}
}
@Override
public void onConnectionSuspended( int i ){
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed( ConnectionResult connectionResult ){
if( connectionResult.hasResolution() ){
try {
// Start an Activity that tries to …Run Code Online (Sandbox Code Playgroud)