php - 如何使用 json api 将 Lat Long 转换为 php 中的地址?

标签 php mysql json file-get-contents geocode

This is my code to get address off particular lat long dynamically from db.If i used this static value it works fine. but if i used $latlong this variable to get dynamic values. it shows error. what's the solution for this. please help me with the same.

注意:C:\xampp\htdocs\demo_calLatLong\calLatLong.php 第 21 行中 undefined offset :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>

最佳答案

您正在尝试访问 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);
?>

关于php - 如何使用 json api 将 Lat Long 转换为 php 中的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42778184/

相关文章:

php - 可以合并4张 table 吗?

java - org.apache.struts2.json.JSONException : org. hibernate.LazyInitializationException:未能延迟初始化集合

php - 超出内存大小?

php - 使用 MySQL 表 PHP 中的数据填充表

mysql - 我需要一些帮助来实现急切加载

mysql - 收藏数据库中的东西 - 最有效的跟踪方法?

php - 非常简单的 SQL 函数返回 bool 值 1 (true) 而不是资源

json - REST API 状态是整数还是字符串?

java - json对象与java对象不一致时的gson.fromJson

php - 重复多对多关系