【问题标题】:PHP Google Maps Api returns ZERO_RESULT in valid requestPHP Google Maps Api 在有效请求中返回 ZERO_RESULT
【发布时间】:2023-04-03 00:04:01
【问题描述】:

我正在做项目,我需要用地址的 lat lng 填充我的数据库。我决定使用 PHP 和地理编码,但我遇到了大问题。 PHP 脚本返回零结果,但是当我尝试使用网络浏览器时,我得到了正确的结果。

$fulladdress = $street.$city;
$request= "http://maps.googleapis.com/maps/api/geocode/xml?address=".rawurlencode($fulladdress)."&sensor=true";
$xml = simplexml_load_file($request) or die("url not loading");

当我打印 $fulladdress、$request 和 xml 状态时,我有这个:

?apino Kartuskie http://maps.googleapis.com/maps/api/geocode/xml?address=%3Fapino%20Kartuskie&sensor=true STATUS: ZERO_RESULTS

【问题讨论】:

    标签: php google-maps


    【解决方案1】:

    事实证明,如果来自 google maps api 的查询/响应有一些变音符号,则必须包含 http 标头“Accept-Language”。就我而言:

    不起作用:

    curl -s 'http://maps.googleapis.com/maps/api/geocode/json?address=ul.+Zwyciestwa+44%2C+Dobre+Miasto'
    

    有效:

    curl -s \
    'http://maps.googleapis.com/maps/api/geocode/json?address=ul.+Zwyciestwa+44%2C+Dobre+Miasto' \
    -H 'Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4'
    

    【讨论】:

      【解决方案2】:

      需要翻译波兰语字符,可以使用http://php.net/manual/en/function.iconv.php

      $fulladdress = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $street.$city);
      $request= "http://maps.googleapis.com/maps/api/geocode/xml?address=".rawurlencode($fulladdress)."&sensor=true";
      $xml = simplexml_load_file($request) or die("url not loading");
      

      【讨论】:

      • 我改变了编码(就像你展示的那样)但结果是一样的。
      【解决方案3】:

      您需要使用 UTF-8 进行编码。

      http://maps.googleapis.com/maps/api/geocode/xml?address=%C5%81apino%20Kartuskie&sensor=true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-11
        • 2023-03-26
        • 1970-01-01
        • 2018-07-05
        • 1970-01-01
        • 1970-01-01
        • 2019-03-13
        • 1970-01-01
        相关资源
        最近更新 更多