Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get more than 20 results ie. 60 using google place API with PHP?

I am developing an google places API application where my target to get all available details of a specific query. But as I know google places API only returns 20 results at a time.But I need all available place details.

If I search with a search keyword more than once its always gives the same results. So I want to know that is there any way to get different results each time I search with the same keyword.

 <?php 
        $placeSearchURL = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=' . $query . '&sensor=true' . '&key=' . API_KEY;

        $placeSearchJSON = file_get_contents($placeSearchURL);
        $dataArray = json_decode($placeSearchJSON);
        $references = array();
        $detailsOfPlaces = array();

        if (isset($dataArray->status)) {
            switch ($dataArray->status) {
                case 'OK' :
                    foreach( $dataArray->results as $details) {
                        $references[] = $details->reference;
                    }
                    break;
                default:
                    echo "Something wrong";
            }
        }

        foreach ($references as $reference) {
            $placeDetailsURL = 'https://maps.googleapis.com/maps/api/place/details/json?reference=' . $reference . '&sensor=true&key=' . API_KEY;
            $placeDetails =file_get_contents($placeDetailsURL);
            $placeDetailsAsArray = json_decode($placeDetails);

            switch ($dataArray->status) {
                case 'OK' :
                    $detailsOfPlace['business_name'] = isset($placeDetailsAsArray->result->name) ? $placeDetailsAsArray->result->name : '';
                    $detailsOfPlace['address'] = isset($placeDetailsAsArray->result->formatted_address) ? $placeDetailsAsArray->result->formatted_address : '';                      

                    $detailsOfPlaces[] = $detailsOfPlace;
                    break;
                default:
                    echo "Something went wrong";
            }
        }

This is my code. It returns 20 results for each query. Can anyone please tell me what adjustment I should made to get 60 results for each query.

Thanks

like image 616
user1559230 Avatar asked Dec 11 '12 05:12

user1559230


People also ask

How can I get more than 20 results on Google Places API?

The documentation says that the Places API returns up to 20 results. It does not indicate that there is any way to change that limit. So, the short answer seems to be: You can't. Of course, you might be able to sort of fake it by doing queries for several locations, and then merging/de-duplicating the results.

How do I get more than 5 reviews on Google Places API?

In order to have access to more than 5 reviews with the Google API you have to purchase Premium data Access from Google. That premium plan will grant you access to all sorts of additional data points you have to shell out a pretty penny.

How many requests are free in Google Maps API?

While there is no maximum number of requests per day, the following usage limits are in place for the Maps JavaScript API: 30,000 requests per minute. 300 requests per minute per IP address. In the Google Cloud Console, this quota is referred to as Map loads per minute per user.


2 Answers

The Google Places API can return up to 60 results per search if available split over three pages of up to 20 results. If more than 20 results are available, you can access the additional results by passing the next_page_token to the pagetoken parameter of a new search request.

For more information about addition results see the Accessing Addition Results section of Place Search page in the documentation.

If you only need place locations you can use the Places API Radar Search to return up to 200 place locations.

EDIT: In your example you do not need to send a details request for each place to access the place name and address. This information is returned in the initial response and can be accessed like the example below:

foreach($dataArray->results as $place) {
  echo '<p><b>' . $place->name . '</b>, ' . $place->formatted_address . '</p>';
}

You can find out if additional pages are available for a Nearby Search request is the next_page_token is returned. This can be checked like below:

if (isset($dataArray->next_page_token)) {
  //get next page
}

If the next_page_token is returned you can get the next page of results by passing the next_page_token to the pagetoken parameter of a new search request or appending it to the end of your original request. Below is and example using a new request:

$placeSearchURL = 'https://maps.googleapis.com/maps/api/place/textsearch/json?nextpage=' . $dataArray->next_page_token . '&sensor=true' . '&key=' . API_KEY;

As the documentation states:

Each page of results must be displayed in turn. Two or more pages of search results should not be displayed as the result of a single query.

Therefore there is a short delay between when a next_page_token is issued, and when it will become valid.

like image 105
Chris Green Avatar answered Nov 15 '22 16:11

Chris Green


As Chris Green says in his answer you will have to use pagination to get more than 20 results. Also as Chris Green also states to take the address you dont really need to make another call to the reference of each place.I am assuming that you will need more information so I just created 2 function implementing what Chris says in his response. Not tested so might be some errors but you can get the feeling.

<?php

    function getAllReferences($query, $maxResults= 60, $nextToken = false) {
        $references = array();
        $nextStr = "";
        if ($nextToken)
            $nextStr = "nextpage=$nextToken";
        $placeSearchURL = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=$query&sensor=true&key=".API_KEY."&$nextStr";
        $placeSearchJSON = file_get_contents($placeSearchURL);
        $dataArray = json_decode($placeSearchJSON);
        if (isset($dataArray->status) &&$dataArray->status == "OK") {
            foreach( $dataArray->results as $details) {
                array_push($references, $details->reference);
            }
            if (!empty($dataArray->next_page_token) && count($references) < $maxResults ) {
                $nextArray = getAllReferences($query, $maxResults-20 , $dataArray->next_page_token);
                $references = array_merge($references, $nextArray);

            }
            return $references;
        }
    }

    function getPlaceFromReference($reference) {
        $placeDetailsURL = 'https://maps.googleapis.com/maps/api/place/details/json?reference=' . $reference . '&sensor=true&key='.API_KEY;
        $placeDetailsContent =file_get_contents($placeDetailsURL);
        $placeDetails = json_decode($placeDetailsContent);
        if ($placeDetails->status == "OK")
            return $placeDetails;
        else return false;
    }

    $references = getAllReferences("test");
    if (count($references) == 0)
        echo "no results";
    else {
        foreach ($references as $reference) {
            $placeDetails = getPlaceFromReference($reference);
            print_r($placeDetails);
        }
    }
like image 44
tix3 Avatar answered Nov 15 '22 18:11

tix3