【问题标题】:PHP - Google Maps API search return ZERO_RESULTS but Navigator show itPHP - Google Maps API 搜索返回 ZERO_RESULTS 但导航器显示它
【发布时间】:2017-08-14 22:32:23
【问题描述】:

我有点问题,

我需要从道路名称(我提供的)中获取一个地方的坐标,我尝试使用 file_get_contentsjson_decode 来完成,但其中一些不起作用(返回一个 stdClass ZERO_RESULTS)!

如果我在浏览器中复制粘贴网址(完全相同),它会向我发送正确的数据,不知道为什么

如果我尝试使用另一个道路名称,它会起作用。

显然我已经尝试清除缓存,但没有结果,更改导航器也不起作用,也在多个服务器上尝试过,但仍然没有结果,尝试使用 CURL (as provided as an answer in a post relating the same problem),但仍然,没有结果。

我很迷茫,希望你能帮我解决这个问题。

这里是代码。

提供的数据来自法国路名,想看的可以在$a中加“法国”

数据

// Not Working
$a = "17 bis Les Chaises";
$b = "77510";

// Working
$a = "2 rue Jean Moulin";
$b = "70300";

生成的链接

// Not Working
https://maps.google.com/maps/api/geocode/json?address=17+bis+Les+Chaises+77510

// Working
https://maps.google.com/maps/api/geocode/json?address=2+rue+Jean+Moulin+70300

卷曲

$url = "https://maps.google.com/maps/api/geocode/json?address=".str_replace(" ","+",$a)."+".$b;
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

$response = curl_exec($ch);

curl_close($ch);

$response_a = json_decode($response);

print_r($response_a);

file_get_contents

$j = json_decode(file_get_contents("https://maps.google.com/maps/api/geocode/json?address=".str_replace(" ","+",$a)."+".$b),true);

print_r("https://maps.google.com/maps/api/geocode/json?address=".str_replace(" ","+",$a)."+".$b);echo "\n\n\n\n\n\n\n";
print_r($j["results"][0]["geometry"]["location"]["lat"].",".$j["results"][0]["geometry"]["location"]["lng"]);

var_dump(file_get_contents("https://maps.google.com/maps/api/geocode/json?address=".str_replace(" ","+",$a)."+".$b));

感谢阅读。

【问题讨论】:

    标签: php google-maps curl google-maps-api-3


    【解决方案1】:

    我发现如果您将 +France 添加到您的搜索条件中,您的非工作请求将不起作用。

    // Not Working
    https://maps.google.com/maps/api/geocode/json?address=17+bis+Les+Chaises+77510 
    
    // Working
    https://maps.google.com/maps/api/geocode/json?address=17+bis+Les+Chaises+77510+France
    

    【讨论】:

    • 在 php 中使用 file_get_contents?
    • 看起来工作正常,不知道为什么。您知道为什么我必须添加“+France”才能工作吗?
    • 没有法国搜索描述符的地址对于谷歌来说似乎太模糊了。如果可以的话,我至少会尝试添加国家代码。另外,ZIP 可能会被误认为是美国。
    • 好的,提醒一下似乎很有用。无论如何,非常感谢您的帮助;D
    猜你喜欢
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2020-08-07
    • 2019-10-28
    相关资源
    最近更新 更多