gpt4 book ai didi

java - getPlaceById() 无法返回 Android 上的数据 google place API/resultCallback 未被调用

转载 作者:行者123 更新时间:2023-12-01 11:38:04 27 4
gpt4 key购买 nike

我无法在 Android 上使用 Google Places API 检索地点详细信息。我正在为特定 place_id 调用 getPlacyById()。

在 Android Activity 中,我设置了 googleApiClient 并调用 connect()。 apiClient连接成功。

然后出于测试目的,我使用硬编码的 place_id 和 resultCallback 调用 getPlaceById()。但是, resultCallback 永远不会被调用。

硬编码的 place_id 通过 JavaScript 进行测试(使用 JavaScript Google Maps API)并返回有效数据。但下面的 Java 代码无法返回任何数据。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_activity_layout);
ButterKnife.inject(this);
if (getActionBar() != null) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}

if (mGoogleApiClient == null) {
rebuildGoogleApiClient();
}
.....
code_for_activity_init
.....
}

protected synchronized void rebuildGoogleApiClient() {
// When we build the GoogleApiClient we specify where connected and connection failed
// callbacks should be returned, which Google APIs our app uses and which OAuth 2.0
// scopes our app requests.
mGoogleApiClient = new GoogleApiClient.Builder(this)
//.enableAutoManage(this, 0 /* clientId */, this)
.addConnectionCallbacks(this)
.addApi(Places.GEO_DATA_API)
.build();
}

@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}

@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}


@Override
public void onConnected(Bundle bundle) {
// Successfully connected to the API client. Pass it to the adapter to enable API access.
//addressesAdapter.setGoogleApiClient(mGoogleApiClient);
Log.i("place api conn", "GoogleApiClient connected.");

}

@Override
public void onConnectionSuspended(int i) {
// Connection to the API client has been suspended. Disable API access in the client.
//mAdapter.setGoogleApiClient(null);
Log.e("place api conn", "GoogleApiClient connection suspended.");
}

@OnLongClick(R.id.one_more_request)
public boolean getDataAgain(){
Log.d("place", "getPlace By Id");
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, "ChIJIV6Mkke9oRQRcPq3KAx513M");
placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
return true;
}

private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
= new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (!places.getStatus().isSuccess()) {
// Request did not complete successfully
Log.e("place", "Place query did not complete. Error: " + places.getStatus().toString());

return;
}
// Get the Place object from the buffer.
final Place place = places.get(0);

// Format details of the place for display and show it in a TextView.
//mPlaceDetailsText.
Toast.makeText(MyActivity.this,/*setText(*/
formatPlaceDetails(getResources(), place.getName(),
place.getId(), place.getAddress(), place.getPhoneNumber(),
place.getWebsiteUri()), Toast.LENGTH_LONG).show();

Log.i("place", "Place details received: " + place.getName());
}
};

这个问题类似于Google Places Api resultCallback not firing但是我检查过我的 AndroidManifest 已经包含所需的权限

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

最终,我希望每次用户从自动完成地址字段中选择一个项目时都调用 getPlaceById() 。

***** 更新 *****找到解决方案。我错过了两件事。首先,这次将maps.v2.API_KEY复制粘贴为geo.API_KEY(并确保删除maps.v2.API_KEY,因为它不能指定两次)

android:name="com.google.android.maps.v2.API_KEY"android:value="whatEverGoogleMapsApiKeyYouHave"

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="whatEverGoogleMapsApiKeyYouHave"/>

其次,我必须启用适用于 Android 的 Google Places API

最佳答案

SDK 21 更新

如果您同时包含这两个元数据,您将收到以下错误:

 Caused by: java.lang.RuntimeException: The API key can only be specified once.

只需包含地理 API key 即可:

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="whatEverGoogleMapsApiKeyYouHave"/>

而不是 map

关于java - getPlaceById() 无法返回 Android 上的数据 google place API/resultCallback 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com