3

我想使用 FusedLocationProviderApi 来获取最后一个已知位置。我按照谷歌教程所说的那样做了,但似乎 GoogleApiClient 没有连接,至少它没有触发 onConnected 或 onConnectionFailed 方法。我希望你能帮助我,在此先感谢。

import com.example.stoos.data.DataModelOrt;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
.
.
.
public class OrtHinzufugen extends Activity  implements
ConnectionCallbacks, OnConnectionFailedListener{
.
.
.
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ort_auswahlen);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    buildGoogleApiClient();
.
.
.
}
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
}
.
.
.
@Override
public void onConnectionFailed(ConnectionResult arg0) {
    System.out.println("Failed");

}

@Override
public void onConnected(Bundle connectionHint) {
    System.out.println("check1?");
    Location mLastLocation;
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        if(mLastLocation.getAccuracy() > 100){
            Toast.makeText(getApplicationContext(), "Für bessere Ergebnisse GPS anmachen", Toast.LENGTH_LONG).show();
        }
        Intent ServiceIntent = new Intent(getApplicationContext(),
                SendeAnfrageService.class);
        ServiceIntent.putExtra("LATITUDE", mLastLocation.getLatitude());
        ServiceIntent.putExtra("LONGITUDE", mLastLocation.getLongitude());
        startService(ServiceIntent); 
        System.out.println("check2?");
    }else{
        Toast.makeText(getApplicationContext(), "Probleme beim laden der Orte. Versuche es bitte nocheinmal", Toast.LENGTH_LONG).show();
    }

}

@Override
public void onConnectionSuspended(int arg0) {
    // TODO Auto-generated method stub

}
4

1 回答 1

7

你错过了

mGoogleApiClient.connect();

于 2015-01-22T23:19:04.663 回答