【发布时间】:2015-06-30 08:18:58
【问题描述】:
我无法在 Android 上使用 Google Places API 检索地点详细信息。我正在为特定的 place_id 调用 getPlacyById()。
在一个 Android 活动中,我设置了一个 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
【问题讨论】:
标签: java android google-maps google-places-api