没有从地方ID获取Google地方信息照片。 Api密钥无效?

时间:2017-02-28 01:58:40

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

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

public class RestaurantList extends 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 for Android”。提前致谢。 Debugging image

1 个答案:

答案 0 :(得分:0)

你没有包含你的清单,所以我只能猜测,但从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" />

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