googlemaps / google-maps-services-java

Java client library for Google Maps API Web Services
Apache License 2.0
1.71k stars 946 forks source link

The Places Api returns only 60 results #624

Closed sriramkp closed 4 years ago

sriramkp commented 4 years ago

The below code returns only 60 results, kindly help me to fetch all the results for any query given.

Code example

GeoApiContext context = new GeoApiContext.Builder()
                    .apiKey(GOOGLE_PLACE_API_KEY)
                    .build();

PlacesSearchResponse results = PlacesApi.textSearchQuery(context, SEARCH_TEXT).await();
while (true) {

    for (PlacesSearchResult result : results.results) {

        PlaceDetails placeDetails = PlacesApi.placeDetails(context, result.placeId).await();

        System.out.println("Name: " + placeDetails.name);
        System.out.println("Rating: " + placeDetails.rating);
        System.out.println("Review Count: " + placeDetails.userRatingsTotal);
        System.out.println("Phone: " + placeDetails.formattedPhoneNumber);
        System.out.println("International Phone: " + placeDetails.internationalPhoneNumber);
        System.out.println("City: " + placeDetails.addressComponents[3].longName);
        System.out.println("Address: " + placeDetails.formattedAddress);
        System.out.println("Website: " + placeDetails.website);
        System.out.println("Google URL: " + placeDetails.url);
        System.out.println("ID: " + placeDetails.placeId);
    }

    if (results.nextPageToken == null)
        break;

    sleep(3000);
    results = PlacesApi.textSearchNextPage(context, results.nextPageToken).await();
}
jpoehnelt commented 4 years ago

This is a limitation of the API to avoid scraping. It never returns more than 60 results for a given query.

By default, each Nearby Search or Text Search returns up to 20 establishment results per query; however, each search can return as many as 60 results, split across three pages. If your search will return more than 20, then the search response will include an additional value — next_page_token.

https://developers.google.com/places/web-service/search#PlaceSearchPaging

sriramkp commented 4 years ago

If it is to avoid scraping for developers, why google maps is giving more than 60 results for users? Avoid scraping for users and not to the developers.