0

我有以下逻辑:

服务器端进行地点搜索网络服务调用并获取有效的唯一跨 Google 地图 API 的PlaceId字符串。检查:从他那里手动构造地点详细信息PlaceId的 URL(返回)返回预期的结果Place,表明他在 Google 的地点数据库中有照片(photos[]结果 Json 不为空......)。

因此,现在服务器将此发送到 Android 客户端,并在 android 中我对Places PhotosPlaceId进行 API 调用以获取至少一张图像。它应该可以工作,因为它是所有谷歌地图产品的唯一地点标识符。工作流程基于 Google 官方示例非常简单,如代码中的注释所示,连接部分成功,所有 API 设置都很好(在开发控制台中,清单等)。PlaceIdAPI 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;
}
4

1 回答 1

2

我不同意错误评论 - photoMetadataBuffer.getCount() == 0 Always。这是一些显示成功加载照片的代码。请注意,就上下文而言,此代码是在 Android Studio Google Maps 模板应用程序中构建的,以便更轻松地将所有 API 密钥配置正确插入应用程序,并具有用于显示结果的地图的好处。

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");
            }
        }
    });
}
于 2015-10-16T05:25:42.093 回答