【发布时间】:2016-01-20 05:32:43
【问题描述】:
我使用谷歌地图检测当前位置但出错 这是我的代码:
private void initGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
})
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
guessCurrentPlace();
}
@Override
public void onConnectionSuspended(int i) {
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
})
.build();
}
private void guessCurrentPlace() {
PendingResult<PlaceLikelihoodBuffer> result =
Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
if (!likelyPlaces.getStatus().isSuccess()) {
// Request did not complete successfully
GooglePlayServicesUtil.getErrorDialog(
likelyPlaces.getStatus().getStatusCode(), ServiceProviderActivity.this ,0).show();
Logger.d(TAG, "Place query did not complete. Error: " + likelyPlaces.getStatus().toString());
likelyPlaces.release();
return;
}
PlaceLikelihood placeLikelihood = likelyPlaces.get(0);
String content = "";
if (placeLikelihood != null && placeLikelihood.getPlace() != null &&
!TextUtils.isEmpty(placeLikelihood.getPlace().getName())) {
content = "Most likely place: " + placeLikelihood.getPlace().getName() + "\n";
}
if (placeLikelihood != null) {
content += "Percent change of being there: " + (int) (placeLikelihood.getLikelihood() * 100) + "%";
}
likelyPlaces.release();
}
});
}
我总是得到错误,并且 possiblePlaces 的值是: {status=Status{statusCode=INTERNAL_ERROR, resolution=null}, attributions=null}
我将这些添加到我的 AndroidManifest.xml 中
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
和
<!-- PlacePicker also requires OpenGL ES version 2 -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
和其他需要的使用权限。 有谁知道为什么?
编辑1: 在 Google Developer Console 中,我获取 API 的统计数据,显示所有响应都是“客户端错误 (4XX)”
编辑2: 我的错误,使用发布密钥库生成 KEY 但使用调试密钥库进行调试。 问题解决了。
【问题讨论】: