如何使用json api将Lat Long转换为php中的地址? [英] How to convert Lat Long to address in php with json api?

查看:140
本文介绍了如何使用json api将Lat Long转换为php中的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,用于动态地从特定纬度处获取地址 从数据库.如果我使用此静态值,它工作正常.但是如果我用过 $ latlong此变量获取动态值.它显示错误. 有什么解决方案.请同样帮我.

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.

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

Notice: Undefined offset: 0 in C:\xampp\htdocs\demo_calLatLong\calLatLong.php on line 21 Notice: Trying to get property of non-object in C:\xampp\htdocs\demo_calLatLong\calLatLong.php on line 21

<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直接访问文件.

You are trying to access url not a file, Use CURL instead to access file direct.

替换

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

这篇关于如何使用json api将Lat Long转换为php中的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆