我使用谷歌地图检测当前的地方,但得到错误 这是我的代码:
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();
}
});
}
我总是得到错误,而且MaybePlaces的值是: {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" />
和其他使用权限。 有谁知道为什么?
EDIT1: 在Google Developer Console中,我获取了API的统计数据,显示所有响应都是“客户端错误(4XX)”
EDIT2: 我的错误,使用release keystore生成KEY,但使用debug keystore调试。 问题解决了。
答案 0 :(得分:0)
我遇到了同样的问题,Place Detection API正在返回INTERNAL_ERROR,我后来发现这是因为Android中的新权限。
我请求了许可,但它确实有效。我希望它的帮助
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions( this, new String[] { Android.Manifest.permission.ACCESS_COARSE_LOCATION }, 2 );
}