我有以下逻辑:
服务器端进行Place Search网络服务调用并获取有效的唯一跨Google地图API的PlaceId
字符串。检查:从他手动构建Place Details的网址(返回PlaceId
)会返回预期的Place
,表示他在Google的地方有照片数据库{photos[]
在结果中Json不是空...)。
所以现在服务器向Android客户端发送此PlaceId
,在android中我向Places Photos发出API调用以获取至少一个图像。它应该有效,因为PlaceId
是所有Google地图产品中唯一的地点标识符。基于官方Google示例,工作流程非常简单,并且如代码中的注释所示,连接部分正在成功并且所有API设置都很好(在开发控制台中,显示API KEY
等) 。
问题是它无法正常工作:在开发控制台中,我看到使用次数增加,并且在我希望获取图像的代码中的位置没有任何反应 - 结果是我尝试了任何PlaceId
(和如上所述应该有照片)谷歌的数据库中没有任何照片。也不是例外或类似这样的调试。问题出在哪儿?谢谢,
//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;
}
答案 0 :(得分:2)
我不同意错误评论 - photoMetadataBuffer.getCount() == 0 Always
。以下是一些显示成功加载照片的代码。请注意,对于上下文,此代码是在Android Studio Google地图模板应用内部构建的,以便更轻松地将所有API Key配置正确插入应用程序中,并获得显示结果的地图奖励。
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private final String placeId = "ChIJhSxoJzyuEmsR9gBDBR09ZrE";
private final static String TAG = "MapsActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId).setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (places.getStatus().isSuccess() && places.getCount() > 0) {
final Place myPlace = places.get(0);
Log.i(TAG, "Place found: " + myPlace.getName());
mMap.addMarker(new MarkerOptions()
.position(myPlace.getLatLng())
.title(myPlace.getName().toString())
.snippet(myPlace.getAddress().toString()));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myPlace.getLatLng()));
}
places.release();
}
});
Places.GeoDataApi.getPlacePhotos(mGoogleApiClient, placeId).setResultCallback(new ResultCallback<PlacePhotoMetadataResult>() {
@Override
public void onResult(PlacePhotoMetadataResult placePhotoMetadataResult) {
if (placePhotoMetadataResult.getStatus().isSuccess()) {
PlacePhotoMetadataBuffer photoMetadata = placePhotoMetadataResult.getPhotoMetadata();
int photoCount = photoMetadata.getCount();
for (int i = 0; i<photoCount; i++) {
PlacePhotoMetadata placePhotoMetadata = photoMetadata.get(i);
final String photoDetail = placePhotoMetadata.toString();
placePhotoMetadata.getScaledPhoto(mGoogleApiClient, 500, 500).setResultCallback(new ResultCallback<PlacePhotoResult>() {
@Override
public void onResult(PlacePhotoResult placePhotoResult) {
if (placePhotoResult.getStatus().isSuccess()) {
Log.i(TAG, "Photo "+photoDetail+" loaded");
} else {
Log.e(TAG, "Photo "+photoDetail+" failed to load");
}
}
});
}
photoMetadata.release();
} else {
Log.e(TAG, "No photos returned");
}
}
});
}