我有以下逻辑:
服务器端进行地点搜索网络服务调用并获取有效的唯一跨 Google 地图 API 的PlaceId
字符串。检查:从他那里手动构造地点详细信息PlaceId
的 URL(返回)返回预期的结果Place
,表明他在 Google 的地点数据库中有照片(photos[]
结果 Json 不为空......)。
因此,现在服务器将此发送到 Android 客户端,并在 android 中我对Places PhotosPlaceId
进行 API 调用以获取至少一张图像。它应该可以工作,因为它是所有谷歌地图产品的唯一地点标识符。工作流程基于 Google 官方示例非常简单,如代码中的注释所示,连接部分成功,所有 API 设置都很好(在开发控制台中,清单等)。PlaceId
API KEY
问题是它不起作用:在开发控制台中,我看到使用次数增加,并且在我希望获取图像的代码中没有任何反应 - 结果是PlaceId
我尝试的任何东西(如上所述应该有照片)在 Google 的数据库中没有任何照片。也不是要调试的异常或类似的东西。问题出在哪里?谢谢,
//Under Activity extends FragmentActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks
private GoogleApiClient mGoogleApiClient = null;
private boolean isConnected = false;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
/* Regular code... */
//Next 2 lines: I don't for what they are good - But in the official Google sample
//they appears - So probably it's right...
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
if (mGoogleApiClient == null) {
rebuildGoogleApiClient();
}
if (isConnected) {
/* This is printed! */
Log.e("TAG", "Start of API Call");
String placeId = /* Call to my server, the result is 100% valid PlaceId that has photos*/
placePhotosTask(placeId, 800, 400); //Height, Width
}
}
private void placePhotosTask(final String placeId, final int width, final int height) {
new PhotoTask(width, height) {
@Override
protected void onPreExecute() {
//TODO Display a temporary image to show while bitmap is loading.
//mImageView.setImageResource(R.drawable.empty_photo);
}
@Override
protected void onPostExecute(AttributedPhoto attributedPhoto) {
if (attributedPhoto != null) {
/* THIS IS NOT PRINTED */
Log.e("TAG", "Success of API Call");
mImageView.setImageBitmap(attributedPhoto.bitmap);
}
}
}.execute(placeId);
}
private class PhotoTask extends AsyncTask<String, Void, PhotoTask.AttributedPhoto> {
private int mHeight;
private int mWidth;
public PhotoTask(int width, int height) {
mHeight = height;
mWidth = width;
}
//Loads the first photo for a place id from the Geo Data API. The place id must be the first (and only) parameter.
@Override
protected AttributedPhoto doInBackground(String... params) {
if (params.length != 1) {
return null;
}
final String placeId = params[0];
AttributedPhoto attributedPhoto = null;
PlacePhotoMetadataResult result = Places.GeoDataApi.getPlacePhotos(mGoogleApiClient, placeId).await();
if (result.getStatus().isSuccess()) {
PlacePhotoMetadataBuffer photoMetadataBuffer = result.getPhotoMetadata();
//BUG!: photoMetadataBuffer.getCount() == 0 Always!
if (photoMetadataBuffer.getCount() > 0 && !isCancelled()) {
//Get the first bitmap and its attributions.
PlacePhotoMetadata photo = photoMetadataBuffer.get(0);
CharSequence attribution = photo.getAttributions();
//Load a scaled bitmap for this photo.
Bitmap image = photo.getScaledPhoto(mGoogleApiClient, mWidth, mHeight).await().getBitmap();
attributedPhoto = new AttributedPhoto(attribution, image);
}
//Release the PlacePhotoMetadataBuffer.
photoMetadataBuffer.release();
}
return attributedPhoto;
}
//Holder for an image and its attribution.
class AttributedPhoto {
public final CharSequence attribution;
public final Bitmap bitmap;
public AttributedPhoto(CharSequence attribution, Bitmap bitmap) {
this.attribution = attribution;
this.bitmap = bitmap;
}
}
}
protected synchronized void rebuildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, 0 /* clientId */, this)
.addConnectionCallbacks(this)
.addApi(Places.GEO_DATA_API)
.build();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
isConnected = false;
}
@Override
public void onConnected(Bundle bundle) {
isConnected = true;
}
@Override
public void onConnectionSuspended(int i) {
isConnected = false;
}