php - 如何在实时服务器中执行 json 解码?

标签 php javascript google-maps json

我目前正在使用谷歌地图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..

PHP.net function json-decode

$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/

相关文章:

javascript - 如何模拟json数据进行单元测试?

雪花中的Javascript函数将当前日期附加到表名

java - 将大图像加载到 Google map 中

javascript - 如何通过 Google Maps API 找到最近的十字路口?

php图像和文本上传不起作用

php - 网站导航

php - 用 PHP 更新 mysql 行的最佳策略?

php - 如何使用 PHP 从 HTML 文档中去除所有 javascript?

ios - google map sdk 中是否有任何选项可以在 IOS 的 google map 底部显示比例尺选项?

php - 如何将生成的图像文件移动到 PHP 中的自定义目录?