18

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

user1559230
  • 2,790
  • 5
  • 26
  • 32

2 Answers2

16

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.

Chris Green
  • 4,397
  • 1
  • 23
  • 21
8

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);
        }
    }
tix3
  • 1,142
  • 8
  • 17