How to get more than 20 results for nearby places using google api

时间:2015-06-30 13:25:28

标签: php pagination google-places-api

<?php
{
    $references = array();
    $names = array();

    $lat="26.177194999999998";
    $long="91.77591333333334";
    $count=0;

    $placeSearchURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=10000&types=mosque&sensor=true&key=AIzaSyASP02W3Qwb75Ruep3isiGstqA7Y2HXjGw";

    $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);
            array_push($names, $details->name);
        }
    }        
    foreach ($names as $name) {
        echo $name."<br>";
    }    
    echo "next token".$dataArray->next_page_token."<br>";
    if(!empty($dataArray->next_page_token)) {
        echo "in the if statement"."<br>";
        $placeSearchURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=10000&types=mosque&sensor=true&key=AIzaSyASP02W3Qwb75Ruep3isiGstqA7Y2HXjGw&nextpage=".$dataArray->next_page_token;
        $placeSearchJSON = file_get_contents($placeSearchURL);
        //echo "hello";
        $dataArray = json_decode($placeSearchJSON);

        if(isset($dataArray->status) &&$dataArray->status == "OK") {
            foreach( $dataArray->results as $details) {
                array_push($references, $details->reference);
                array_push($names, $details->name);
            }
        }
        echo "hello<br>"; 
    }

    foreach ($names as $name) {
        echo $name."<br>";  
    }
    echo "next token".$dataArray->next_page_token."<br>";

    if(!empty($dataArray->next_page_token)) {
        echo "in the if statement"."<br>";
        $placeSearchURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$long&radius=10000&types=mosque&sensor=true&key=AIzaSyASP02W3Qwb75Ruep3isiGstqA7Y2HXjGw&nextpage=".$dataArray->next_page_token;

        $placeSearchJSON = file_get_contents($placeSearchURL);
        //echo "hello";
        $dataArray = json_decode($placeSearchJSON);
        if(isset($dataArray->status) &&$dataArray->status == "OK") {
            foreach( $dataArray->results as $details) {

            array_push($references, $details->reference);
            array_push($names, $details->name);
        }
    }
    echo "hello<br>";         
}
foreach ($names as $name) {
    echo $name."<br>";
}
?>

I get the same 20 results every time, and I have used pagination as you can see in my code. Every time I print the next page token I get a different one.

I would really appreciate if someone could point out the error in my code because I am stuck with this for quite some time. You are also most welcome to give any kind of relevant suggestion.

2 个答案:

答案 0 :(得分:1)

在你的网址字符串中使用pagetoken而不是nextpage,在最后两次调用中只提供api密钥和页面标记作为参数,并在每次api调用后使用sleep(2)我尝试了你的代码,当下面的更改是made.happy编码!

答案 1 :(得分:0)

<?php

//echo"hello"; 
   $references = array();
     $names = array();

    $lat="26.177194999999998";
    $long="91.77591333333334";
     $count=0;

 $placeSearchURL ="https://maps.googleapis.com/maps/api/place/radarsearch/json?location=26.177194999999998,91.77591333333334&radius=3000&types=mosque&key=your APIkey";
    $placeSearchJSON = file_get_contents($placeSearchURL);

    $dataArray = json_decode($placeSearchJSON);


        foreach( $dataArray->results as $details) {

            //echo $details->place_id;
            array_push($references, $details->place_id);

        }



       foreach($references as $reference)
       {

        $count=$count+1;
        //echo $count."  ";
              $placeSearchURL="https://maps.googleapis.com/maps/api/place/details/json?placeid=".$reference."&key=your apikey";
          $placeSearchJSON = file_get_contents($placeSearchURL);
          $dataArray = json_decode($placeSearchJSON);

          echo $dataArray->result->name."<br>";
          echo "lat  ".$dataArray->result->geometry->location->lat."<br>";
           echo "lon  ".$dataArray->result->geometry->location->lng."<br>";
           echo "address  ".$dataArray->result->formatted_address."<br>";
          echo "<br>";

        }

&GT;

基本上我想找到一种方法来获得超过20个类型的masjid,所以我首先使用雷达搜索来获得placeid然后为每个placement_id获取信息,最好的部分是你可以得到附近地方的200个结果< / p>

相关问题