【问题标题】:Google Geocode API different resultsGoogle Geocode API 不同的结果
【发布时间】:2019-09-19 15:32:38
【问题描述】:

我有一个有点奇怪的问题。 我正在做一个项目,需要知道我的 php 脚本中两个位置之间的距离。 您可以为 google api 提供城市名称或邮政编码。

嘿应该很容易吧?错了!

我当然尝试使用已知的邮政编码和我的城市对其进行测试。 “汉诺威”和“30159”的距离应该在 0 左右,但奇怪的是我超过了 7200 公里。 (我计算两个坐标之间距离的代码相信我)

现在来看实际问题。

我的步骤:

  1. var_dump api 请求的结果
  2. api 请求的回显 url
  3. 在浏览器中打开网址并比较输出

这是我非常简单的代码。

$url = "https://maps.googleapis.com/maps/api/geocode/json?&address=".urlencode($ort)."&key=$google_api_key";
echo $url."<hr>";
$string = file_get_contents($url);
echo $string."<hr>";

现在这很奇怪。与 php 脚本相比,浏览器中的输出完全不同。

output compared browser vs script (sry I have no formatting for the script output)

是否有人能够向我解释和/或提供解决方案以使脚本的输出类似于位于德国境内的邮政编码 30159 的浏览器的“所需”输出?

干杯Nexarius

【问题讨论】:

    标签: php api geocode


    【解决方案1】:

    好的,我想通了,如果有人遇到同样的问题,我想分享我的解决方案。

    $url = "https://maps.googleapis.com/maps/api/geocode/json?&address=".urlencode($ort)."&key=$google_api_key";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ["Accept-Language: de-DE-1996"]); // important
    $string = curl_exec($curl);
    curl_close($curl);
    

    当您进行 ajax 调用时,浏览器会自动传输您的语言,但 php 函数没有。使用 curl,您可以指定您的语言,这会使 google api 报告与您所在地区最相关的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 2018-08-16
      • 1970-01-01
      相关资源
      最近更新 更多