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/