如何使用 google api 获得超过 20 个附近地点的结果

How to get more than 20 results for nearby places using google 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>";
}
?>

我每次都得到相同的 20 个结果,而且我使用了分页,您可以在我的代码中看到。每次我打印下一页标记时,我都会得到一个不同的标记。

如果有人能指出我的代码中的错误,我将不胜感激,因为我已经坚持了很长一段时间。也欢迎大家提出相关建议。

在您的 url 字符串中使用 pagetoken 而不是 nextpage 并且在最后两次调用中仅提供 api 键和页面令牌作为参数并且在每个 api 调用之后使用 sleep(2 ) 我尝试了您的代码,当以下更改为 made.happy 编码时,它可以完美运行!

<?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>";

        }

?>

基本上我想找到一种方法来获得超过 20 个清真寺类型的地方,所以我首先使用雷达搜索来获取 placeids,然后为每个 placed_id 获得信息,最好的部分是你可以得到周围附近地点的 200 个结果