【问题标题】:How to convert Lat Long to address in php with json api?如何使用 json api 将 Lat Long 转换为 php 中的地址?
【发布时间】:2017-03-14 05:13:16
【问题描述】:

这是我动态获取特定纬度地址的代码 来自 db.If 我使用这个静态值它工作正常。但如果我用 $latlong 这个变量来获取动态值。它显示错误。 有什么解决方案。请帮助我。

注意:未定义的偏移量:C:\xampp\htdocs\demo_calLatLong\calLatLong.php 第 21 行中的 0 注意:尝试在第 21 行获取 C:\xampp\htdocs\demo_calLatLong\calLatLong.php 中非对象的属性

<table class="table table-bordered">
        <thead>
            <tr>         
              <th>Client Name</th>                                            
            </tr>
        </thead>
        <?php 
            include 'db.php';
            $sql = 'SELECT  *  FROM `location`';            
            $travel = mysqli_query($conn,$sql);     
        while ($row = mysqli_fetch_array($travel)) 
        {?>         
        <tbody>
            <tr class="active">
            <?php
               $latlong = $row[1].','.$row[2];
               $geocode=json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=19.0978,22.8972&sensor=false',true));            
            ?>  
            <td><?php echo $geocode->results[0]->formatted_address;?></td>                                
            </tr>                               
        </tbody>
    <?php }?>
    </table>

【问题讨论】:

    标签: php mysql json file-get-contents geocode


    【解决方案1】:

    您正在尝试访问 url 而不是文件,请使用 CURL 直接访问文件。

    替换

            <?php
               $latlong = $row[1].','.$row[2];
               $geocode=json_decode(file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=19.0978,22.8972&sensor=false',true));            
            ?> 
    

    <?php
        $url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$row[1],$row[2]&sensor=false";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        $geocode = json_decode($output);
    ?>
    

    【讨论】:

    • 如果我将您的代码与 一起使用,它会显示数据,但我想要格式化的地址。所以我使用了 results[0]->formatted_address;?> 。但是使用它会给我错误通知:尝试在第 27 行的 C:\xampp\htdocs\demo_calLatLong\calLatLong.php 中获取非对象的属性
    • 你得到什么网址?分享试试?
    • 另外分享 print_r($row); 可能是你弄错了 latlong,你可以尝试在你的问题中使用静态值。
    • 如果我将静态值与 print_r 一起使用,它会显示与动态值上的 var_dump 相同的 5 个输出。无法理解我的代码有什么问题。我离我的解决方案太近了,请帮忙。
    • Bhai,分享print_r($row); ?可能您需要使用$row[0]$row[1] 而不是$row[1]$row[2]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多