我目前正在使用谷歌地图API。在谷歌地图API中,我正在从特定地址获取经度和纬度。
这是代码:
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=1746+Esterbrook+Dr&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
在此纬度和经度中,值在我的实时服务器中显示为空(null)。在我的本地主机计算机中,它工作得很好。显示谷歌地图需要包含什么内容吗?
最佳答案
我个人更喜欢返回数组,因为它更明确。您可以通过在 json 解码时将第二个参数设置为 true 来实现此目的。另外,我建议假设它并不总是会返回结果,这就是为什么我添加了额外的 if 语句。按照此 URL..
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=1746+Esterbrook+Dr&sensor=false');
if(strlen($geocode)>0){
$output= json_decode($geocode, true);
if(is_array($output) && count($output)>0)
{
$output[0]['location']['lat'] ;
print_r($output); // the printr will give you a clue for the exact nesting to use above
}
}
关于php - 如何在实时服务器中执行 json 解码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15158202/