返回我有以下邏輯:谷歌Android地方的照片搜索API不工作PlaceId從地方搜索Web服務API
服務器端進行Place Search Web服務調用,並得到有效的獨特的跨谷歌地圖API的PlaceId
字符串。檢查:從他手中構建Place Details的手動URL(返回PlaceId
)會返回預期的Place
,表示他在Google的位置DB中有照片(結果中的photos[]
Json不爲空...)。
所以現在服務器發送到Android客戶端這個PlaceId
和在Android我做API調用Places Photos得到至少一個圖像。它應該可以工作,因爲PlaceId
是所有Google地圖產品中唯一的地點標識符。基於官方的Google示例,工作流程非常簡單直接,如代碼中的註釋所示,連接部分成功並且所有API設置都很好(在開發控制檯中,清單中有API KEY
等)。
問題是它不工作:在dev控制檯中,我看到使用次數增加,並且在代碼中我希望獲取圖像的地方沒有任何反應 - 結果是我嘗試了任何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;
}
它是否僅適用於特定的PlaceID或所有的PlaceID? – Brett