2

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();
            }
    });
D.J
  • 1,439
  • 1
  • 12
  • 23
John Ward
  • 910
  • 2
  • 10
  • 21

2 Answers2

3

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?

Community
  • 1
  • 1
AndrewR
  • 10,759
  • 10
  • 45
  • 56
  • I had read that posting, but I assumed it was equivalent to: String ids = "placeid1, placeid2, placeid3". I didn't try this, but I probably will just to see if it works. String ids = "\"placeid1\",\"placeid2\"" The array is working well and doesn't have the messy escaped quotes, so I'll probably keep using that approach anyway. Thanks. – John Ward Jan 19 '17 at 22:32
0

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);
John Ward
  • 910
  • 2
  • 10
  • 21