android: calling Google API .getPlaceById with multiple place_id's

为君一笑 提交于 2020-07-09 09:17:27

问题


In order to reduce the number of API calls, I'm trying to query place details by passing several place_ids at a time (up to 10). I haven't found any useful information beyond the docs.

https://developers.google.com/android/reference/com/google/android/gms/location/places/GeoDataApi.html#getPlaceById(com.google.android.gms.common.api.GoogleApiClient, java.lang.String...)

Notably, the constructor is:

public abstract PendingResult<PlaceBuffer> getPlaceById (GoogleApiClient client, **String... placeIds**)

and the doc says: Returns Place objects for each of the given place IDs.

I don't have any problem when passing a single place_id, but when I pass a comma delimted string of id's, all of which are known to be good, I get a status = "SUCCESS" but a buffer count of 0.

Does anyone know the correct way to pass multiple id's to getPlaceById()?

Here's my code if that helps at all:

    Places.GeoDataApi.getPlaceById(mGoogleApiClient, searchIds)
        .setResultCallback(new ResultCallback<PlaceBuffer>() {
            @Override
            public void onResult(PlaceBuffer places) {
                int cnt = places.getCount();
                if (places.getStatus().isSuccess() && places.getCount() > 0) {
                    for (int i=0; i < places.getCount(); i++) {
                        final Place myPlace = places.get(i);
                        Log.d("<< cache >> ", "Place found: " + myPlace.getName());
                    }
                } else {
                    Log.d("<< cache >> ", "Place not found");
                }
                places.release();
            }
    });

回答1:


It's a varargs argument. You call it like this:

Places.GeoDataApi.getPlaceById(mGoogleApiClient, 
        "placeId1", "placeId2", "placeId3");

More detail in this SO question: How does the Java array argument declaration syntax "..." work?




回答2:


Although I've read that String... can be passed as either a comma delimited string or a string array, for one reason or other, getPlaceById appears to require an array. When I use this code to prepare the place id parameter, it works fine:

    String search[] = new String[idsToSearch.size()];
    search = idsToSearch.toArray(search);


来源:https://stackoverflow.com/questions/41692319/android-calling-google-api-getplacebyid-with-multiple-place-ids

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