2017-02-28 50 views
1

在調試PhotoMetaDataResult結果參數zzUX時顯示Places_API_KEY_INVALID。 Debugging Image。儘管我在Manifests文件中有「Google Places API Key for Android」。未從地點ID獲取Google地點信息照片。無效的Api密鑰?

公共類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

回答

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" /> 

嘗試改變這種價值你的清單,看看是否有幫助!

+0

就是這樣。謝謝:) – rameesha

+0

沒問題,很高興幫助! – Destry