适用于Android的Google Places API - 为城市

时间:2016-02-09 22:33:44

标签: android google-places-api

我正在使用Google place API for android让用户选择世界各地的城市。然后我使用placeId来获取类似于谷歌地图的图像。

Picture example

我每次都能成功拍摄照片,但不包含图片。如果我使用任何餐厅或咖啡馆的placeId或任何其他单一地点而不是整个城市,它的效果会很好。

这是我用来获取图片的方法。

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

}

1 个答案:

答案 0 :(得分:0)

我在我的android工作室中使用了你的代码,它在你的代码中没有问题。

我猜你忘了在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>

供参考设置你的“Android KEY”
https://developers.google.com/places/android-api/signup

https://console.developers.google.com/apis(此链接可以设置您的密钥)

顺便说一下,你可以使用:

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

检查您的错误消息,希望它能为您提供帮助