我无法获取最后的已知位置.我在Google控制台中启用了Geocoding API和适用于Android的Google Places API.我在清单文件中添加了api密钥:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key" />
Run Code Online (Sandbox Code Playgroud)
但我一直在控制台中收到一条消息:" 无法连接到Google API客户端:ConnectionResult {statusCode = API_UNAVAILABLE,resolution = null} "
UPDATE
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle connectionHint) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
Toast.makeText(this, "Latitude = " + mLastLocation.getLatitude() + "\n" +
"Longitude = " + mLastLocation.getLongitude(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) { …Run Code Online (Sandbox Code Playgroud)