【问题标题】:Not getting Google Places Photos from place id. Invalid Api key?未从地点 ID 获取 Google 地方信息照片。无效的 API 密钥?
【发布时间】:2017-02-28 01:58:39
【问题描述】:

在调试 PhotoMetaDataResult 结果参数 zzUX 时显示 Places_API_KEY_INVALID。 Debugging Image 。虽然我在 Manifests 文件中有“Google Places API Key for Android”。

公共类 RestaurantList 扩展 AppCompatActivity 实现 GoogleApiClient.ConnectionCallbacks、GoogleApiClient.OnConnectionFailedListener{

String placeId;
GoogleApiClient mGoogleApiClient;
protected void onCreate(Bundle savedInstanceState) {

    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Places.GEO_DATA_API)
            .addApi(Places.PLACE_DETECTION_API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
    placePhotosTask();

}

abstract 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();
            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;
        }
    }
}

private void placePhotosTask() {
    final String placeId = "ChIJrTLr-GyuEmsRBfy61i59si0"; // Australian Cruise Group

    // Create a new AsyncTask that displays the bitmap and attribution once loaded.
    new PhotoTask(mImageView.getWidth(), mImageView.getHeight()) {
        @Override
        protected void onPreExecute() {
            // 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) {
                // Photo has been loaded, display it.
                mImageView.setImageBitmap(attributedPhoto.bitmap);

                // Display the attribution as HTML content if set.
                if (attributedPhoto.attribution == null) {
                    mText.setVisibility(View.GONE);
                } else {
                    mText.setVisibility(View.VISIBLE);
                    mText.setText(Html.fromHtml(attributedPhoto.attribution.toString()));
                }

            }
        }
    }.execute(placeId);
}

protected void onDestroy() {
    photoMetadataBuffer.release();
    mGoogleApiClient.disconnect();
    super.onDestroy();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

}

在调试 PhotoMetaDataResult 结果参数 zzUX 时显示 Places_API_KEY_INVALID。虽然我在 Manifests 文件中有“Google Places API Key for Android”。提前致谢。 Debugging image

【问题讨论】:

    标签: android google-places-api api-key


    【解决方案1】:

    你没有包括你的清单,所以我只能猜测,但从the google api docs看来他们已经改变了元数据

    <meta-data
           android:name="com.google.android.geo.API_KEY"
           android:value="@string/google_maps_key" />
    

    <meta-data
             android:name="com.google.android.maps.v2.API_KEY"
             android:value="@string/google_maps_key" />
    

    尝试更改清单中的该值,看看是否有帮助!

    【讨论】:

    • 就是这样。谢谢:)
    • 没问题,很高兴为您提供帮助!
    猜你喜欢
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多