getPlaceById() fails to return data on Android google places API / resultCallback not getting called

浪尽此生 提交于 2019-12-25 02:56:17

问题


I cannot retrieve Place Details using the Google Places API on Android. I'm calling getPlacyById() for specific place_id.

Inside an Android activity I setup a googleApiClient and call connect(). The apiClient connects successfully.

Then for testing purposes I call getPlaceById() using a hardcoded place_id and a resultCallback. However, the resultCallback never gets called.

The hardcoded place_id is tested through javascript (using the javascript Google Maps API) and returns valid data. but the Java code below fails to return any data.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.some_activity_layout);
    ButterKnife.inject(this);
    if (getActionBar() != null) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    if (mGoogleApiClient == null) {
        rebuildGoogleApiClient();
    }
    .....
    code_for_activity_init
    .....
}

protected synchronized void rebuildGoogleApiClient() {
    // When we build the GoogleApiClient we specify where connected and connection failed
    // callbacks should be returned, which Google APIs our app uses and which OAuth 2.0
    // scopes our app requests.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            //.enableAutoManage(this, 0 /* clientId */, this)
            .addConnectionCallbacks(this)
            .addApi(Places.GEO_DATA_API)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}


@Override
public void onConnected(Bundle bundle) {
    // Successfully connected to the API client. Pass it to the adapter to enable API access.
    //addressesAdapter.setGoogleApiClient(mGoogleApiClient);
    Log.i("place api conn", "GoogleApiClient connected.");

}

@Override
public void onConnectionSuspended(int i) {
    // Connection to the API client has been suspended. Disable API access in the client.
    //mAdapter.setGoogleApiClient(null);
    Log.e("place api conn", "GoogleApiClient connection suspended.");
}

@OnLongClick(R.id.one_more_request)
public boolean getDataAgain(){
    Log.d("place", "getPlace By Id");
    PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
            .getPlaceById(mGoogleApiClient, "ChIJIV6Mkke9oRQRcPq3KAx513M");
    placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
    return true;
}

private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
        = new ResultCallback<PlaceBuffer>() {
    @Override
    public void onResult(PlaceBuffer places) {
        if (!places.getStatus().isSuccess()) {
            // Request did not complete successfully
            Log.e("place", "Place query did not complete. Error: " + places.getStatus().toString());

            return;
        }
        // Get the Place object from the buffer.
        final Place place = places.get(0);

        // Format details of the place for display and show it in a TextView.
        //mPlaceDetailsText.
                Toast.makeText(MyActivity.this,/*setText(*/
                        formatPlaceDetails(getResources(), place.getName(),
                place.getId(), place.getAddress(), place.getPhoneNumber(),
                place.getWebsiteUri()), Toast.LENGTH_LONG).show();

        Log.i("place", "Place details received: " + place.getName());
    }
};

this question is similar to Google Places Api resultCallback not firing however I have checked that my AndroidManifest already includes the the required permission

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

Ultimately I would like the getPlaceById() to be called every time the user selects an item from an autocomplete Address field.

***** UPDATE ******* Solution found. I was missing two things. First, copy-paste the maps.v2.API_KEY this time as geo.API_KEY (and make sure you delete the maps.v2.API_KEY, because it cannot be specified twice)

android:name="com.google.android.maps.v2.API_KEY" android:value="whatEverGoogleMapsApiKeyYouHave"

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

and secondly I had to enable Google Places API for Android


回答1:


UPDATE for SDK 21

If you have both meta-data's included you will get the following error:

 Caused by: java.lang.RuntimeException: The API key can only be specified once.

Instead just indlude the geo API key:

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

and NOT the maps one




回答2:


This exactly what I've been struggling with. In Android Studio when I added the 2nd API key I received an exception:

 Caused by: java.lang.RuntimeException: The API key can only be specified once.

This was resolved by just adding the "com.google.android.geo.API_KEY" meta-data entry to the Manifest file and removing the maps one.




回答3:


I was missing two things.

First of all the com.google.android.geo.API_KEY which is the same as the com.google.android.maps.v2.API_KEY. Just copy paste the API KEY on the AndroidManifest file.

android:name="com.google.android.maps.v2.API_KEY" android:value="whatEverGoogleMapsApiKeyYouHave"

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

I was also missing the Google Places API for Android Service on google console. If you have Google Places API enabled , beware, this in not enough, because it's simply a different API. You have to go to google console and explicitly enable the Google Places API for Android.

****** EDIT ******** As @Lissy pointed out, from SDK 21 onwards, defining the same API_KEY twice will give an error. Keeping the geo.API_KEY will suffice



来源:https://stackoverflow.com/questions/29801555/getplacebyid-fails-to-return-data-on-android-google-places-api-resultcallbac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!