Search code examples
androidgoogle-places-apiapi-key

Not getting Google Places Photos from place id. Invalid Api key?


On debugging PhotoMetaDataResult result parameter zzUX shows Places_API_KEY_INVALID. Debugging Image . Although I have "Google Places API Key for Android" in Manifests file.

public class RestaurantList extends AppCompatActivity implements 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();
}

}

On debugging PhotoMetaDataResult result parameter zzUX shows Places_API_KEY_INVALID. Although I have "Google Places API Key for Android" in Manifests file. Thanks in advance. Debugging image


Solution

  • You did not include your manifest, so I can only guess but from the google api docs it seems they have changed the meta data from

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

    to

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

    try changing that value in your manifest and see if that helps!