How to get 20+ result from Google Places API?

60,633

Solution 1

As per the documentation:

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

EDIT: Place API now supports pagination of up to 60 results. See below for details.

The Places API will return up to 20 establishments 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 parameter — next_page_token. Pass the value of next_page_token to the page_token parameter of a new place search to see the next set of 20 results.

There is also strict terms of service that specify you cant Pre-Fetch, Cache, or Store Content unless it is the content identifier or key you are permitted to store i.e. reference token from a places search:

https://cloud.google.com/maps-platform/terms/?_ga=#3-license

In regards to not being able to find ATM's in India. All places are categorized under the type establishment until Google has enough metadata about a place to categorize it under more specific place types like ATM, cafe, bar etc. A work around that may find more results is to use the keyword parameter in your request with something like 'ATM', 'bank' or 'finance' as the value. As per the documentation:

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

The keyword parameter is matched against all available fields, including but not limited to name, type, and address, as well as customer reviews and other third-party content.

Solution 2

Google API fetches the 20 Result in one page suppose you want to use the next page 20 result then we use the next_page_token from google first page xml as a result.

1) https://maps.googleapis.com/maps/api/place/search/xml?location=Enter latitude,Enter Longitude&radius=10000&types=store&hasNextPage=true&nextPage()=true&sensor=false&key=Enter Google_Map_key

in second step you use the first page's next_page_token data

2)https://maps.googleapis.com/maps/api/place/search/xml?location=Enter Latitude,Enter Longitude&radius=10000&types=store&hasNextPage=true&nextPage()=true&sensor=false&key=enter google_map_key &pagetoken="Enter the first page token Value"

Solution 3

You can do this by using Google API JAVA Client - Here is an example using the java client for getting all the 60 results.

public PlacesList search(double latitude, double longitude, double radius, String types)
            throws Exception {

        try {

            HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
            HttpRequest request = httpRequestFactory
                    .buildGetRequest(new GenericUrl("https://maps.googleapis.com/maps/api/place/search/json?"));
            request.getUrl().put("key", YOUR_API_KEY);
            request.getUrl().put("location", latitude + "," + longitude);
            request.getUrl().put("radius", radius); 
            request.getUrl().put("sensor", "false");
            request.getUrl().put("types", types);

            PlacesList list = request.execute().parseAs(PlacesList.class);

            if(list.next_page_token!=null || list.next_page_token!=""){
                         Thread.sleep(4000);
                         /*Since the token can be used after a short time it has been  generated*/
                request.getUrl().put("pagetoken",list.next_page_token);
                PlacesList temp = request.execute().parseAs(PlacesList.class);
                list.results.addAll(temp.results);

                if(temp.next_page_token!=null||temp.next_page_token!=""){
                    Thread.sleep(4000);
                    request.getUrl().put("pagetoken",temp.next_page_token);
                    PlacesList tempList =  request.execute().parseAs(PlacesList.class);
                    list.results.addAll(tempList.results);
              }

            }
            return list;

        } catch (HttpResponseException e) {
            return null;
        }

    }
Share:
60,633

Related videos on Youtube

Scorpion
Author by

Scorpion

Updated on February 06, 2020

Comments

  • Scorpion
    Scorpion about 4 years

    I am developing an app in which I am getting the list of ATM's near by the user. For that I am using Google Places API, but every time it returns 20 result only. I want to get more results. In the API doc it is mention that it will return 20 result but I would like to know is there any way I can get more data or I have to create my own database for that?
    Also what I found in the API is that it does not have all the ATM's for India. The information is also not accurate for some of the locations. So Please suggest me some idea for that I should use my own database or not?
    I know that I can register places in the Google Places but there is the problem that every time it returns only 20 result. If anyhow this problem is resolved then it will be great to use the Places API..

    Please kindly help me.....

    • Bo Persson
      Bo Persson about 12 years
    • Chris Green
      Chris Green about 12 years
      What are you trying to provide for your users that needs more than 20 results at a time? If you provide more details about what you are trying to achieve I may be able to provide a better solution for you.
    • Scorpion
      Scorpion about 12 years
      @ChrisGreen, I would like to provide the near by ATM's to my users but its not that only 20 nearest. I have seen the Places App provided by Google in that when you reach at the end of the list of ATM's (in my case) it shows more results. I wish to display 40-50 results to the user. And the Data which is provided by Google Places API is not accurate for India because I found that lots of ATM's & Banks near by my area is not available in the list. I done some R&D in that and found that i can add places to Places API or I can use my own DB too. What you suggest I should use? I am very confused..
    • Chris Green
      Chris Green about 12 years
      The Places App does not use the Places API therefore it is not limited to 20 results. Try the workaround described below to help find more ATM's. If you are still not having any luck it may be because Google has no data for that area. You can perform a Place Report Request to add the data your self or you can upload bulk data: google.com/local/add/g?gl=US&hl=en-US#uploadfeed Instead of showing your users a large list of all ATM's why not provide them with a selection of banks to search for ATM's for. Using bank name as keyword in your Places Search Request.
    • Scorpion
      Scorpion about 12 years
      @ChrisGreen, Thank you so much for this link & your suggestion. But one more thing i would like to know is that if i upload the data then it will be available through Places API only right? I wish to add more services like Food, Restaurants, Cafes & many more. So in some services i wish to show more than 20+ results.
    • Chris Green
      Chris Green about 12 years
      If adding places through the bulk upload form, after verification uploaded places will be available on all Google maps products. (Verification process can take up to a week). If adding places through the Places API using Place Report Requests, places will be available to your API key instantly, and on all other Google Maps products after verification.
    • Chris Green
      Chris Green about 12 years
      The Places API will not return more than 20 places at a time so you will have to work around this when building your application.
    • Scorpion
      Scorpion about 12 years
      But what other option to resolve this? Do you have any idea/suggestion for this? Because I want to get near by places which i can get from Places API only & it gives me 20 results only.
  • Malik Saad Bin Tariq
    Malik Saad Bin Tariq about 11 years
    this is great. but what if i am using the android api? does it give the next page option? i can't find it.
  • Rushabh
    Rushabh about 11 years
    @ Malik Saad Bin Tariq yes you can find it this way in android google map api also
  • Kushal Suthar
    Kushal Suthar almost 10 years
    can we got the all 60 records at once?
  • Pankaj
    Pankaj over 9 years
    Param for accessing next 20 result should be "pagetoken" with value of next_page_token.
  • Vikrant_Dev
    Vikrant_Dev over 9 years
    where is PlaceList class here?
  • kshepherd
    kshepherd over 8 years
    Thank you - you solved my problem. The next page is not available immediately! I saw your "Thread.sleep(4000);" and realized why pagetoken only sometimes works in my code - usually the code is too quick in requesting the next page. Of course when I debugged it by steping through it always worked.
  • mel
    mel about 7 years
    You can get 60 results making 3 API calls, not one.
  • 25mhz
    25mhz almost 4 years
    is this applicable even for paid users ?
  • Engr Umair
    Engr Umair almost 3 years
    @Chris Can you please let me know how to fetch more than 60 places, is it available in paid api?
  • Prashant
    Prashant almost 3 years
    it is pagetoken= not next_page_token=