简体   繁体   English

适用于Android的Google Places API-为城市获取0张照片

[英]Google Places API for android - getting 0 photos for cities

I am using Google places API for android to let the user select cities in the world. 我正在使用适用于Android的Google Places API让用户选择世界上的城市。 Then I use the placeId to get an image to something similar to google maps. 然后,我使用placeId来获取类似于Google地图的图像。

Picture example 图片示例

Every single time I'm getting the photos bundle successfully but it contains no images. 每次我都成功获取照片捆绑包,但其中不包含图像。 It works just fine if I use the placeId of any restaurant or cafe or any other single spot but not for a whole city. 如果我使用任何餐厅,咖啡馆或任何其他单一地点的placeId,但不适用于整个城市,则效果很好。

Here's the method I'm using to get the images. 这是我用来获取图像的方法。

public void loadPlaceImage(final ImageView imageView, String placeId) {

    /**
     * Load a bitmap from the photos API asynchronously
     * by using buffers and result callbacks.
     */

    Places.GeoDataApi.getPlacePhotos(mGoogleApiClient, placeId)
            .setResultCallback(new ResultCallback<PlacePhotoMetadataResult>() {


                @Override
                public void onResult(PlacePhotoMetadataResult photos) {
                    if (!photos.getStatus().isSuccess()) {
                        Log.d(TAG, "Couldn\'t receive photos bundle successfully.");
                        return;
                    }

                    Log.d(TAG, "Photo bundle received successfully");

                    PlacePhotoMetadataBuffer photoMetadataBuffer = photos.getPhotoMetadata();
                    if (photoMetadataBuffer.getCount() > 0) {
                        // Display the first bitmap in an ImageView in the size of the view
                        photoMetadataBuffer.get(0)
                                .getScaledPhoto(mGoogleApiClient, imageView.getWidth(),
                                        imageView.getHeight())
                                .setResultCallback(new ResultCallback<PlacePhotoResult>() {
                                    @Override
                                    public void onResult(PlacePhotoResult placePhotoResult) {
                                        if (!placePhotoResult.getStatus().isSuccess()) {
                                            Log.d(TAG, "Couldn\'t retrieve the photo successfully.");
                                            return;
                                        }

                                        Log.d(TAG, "Successfully retrieved photo from photo bundle.");

                                        imageView.setImageBitmap(placePhotoResult.getBitmap());
                                    }
                                });
                    } else {
                        Log.d(TAG, "0 images in the buffer.");
                    }
                    photoMetadataBuffer.release();
                }
            });

}

I used your code in my android studio, it hadn't problem in your code. 我在Android Studio中使用了您的代码,但您的代码没有问题。

I guess you forgot to add meta-data in Manifest. 我猜您忘了在清单中添加元数据。

  <application
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOURKEY"/>
</application>

it is for reference to set up your "Android KEY" 供您设置“ Android KEY”以供参考
https://developers.google.com/places/android-api/signup https://developers.google.com/places/android-api/signup

https://console.developers.google.com/apis (this Link can set your key) https://console.developers.google.com/apis (此链接可以设置您的密钥)

By the way, you can use: 顺便说一句,您可以使用:

Log.d(TAG,"result getStatus "+result.getStatus().getStatus()); Log.d(TAG,“ result getStatus” + result.getStatus()。getStatus());

to check your error message, hope it will help you 检查您的错误消息,希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM