使用PHP和API自动获取纬度和经度

25

在我的一个php应用程序中,我需要通过地址找到地点的纬度和经度。

我尝试了这段代码:

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

但是它显示以下错误:

警告:file_get_contents(http://maps.google.com/maps/api/geocode/json?address=technopark,Trivandrun,kerala,India&sensor=false&region=IND)[function.file-get-contents]:无法打开流:HTTP请求失败! HTTP/1.0 400 Bad Request in D:\Projects\lon.php on line 4

请帮我解决这个问题。


3
请不要在您的帖子中添加签名或标语。 - user229044
7个回答

40

使用 curl 替代 file_get_contents

$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;

1
有人知道指定CURLOPT_PROXYPORT 3128的目的吗? - sootsnoot
哎呀 - 评论编辑超过了5分钟的限制...我对使用代理完全不熟悉,但从我阅读的内容来看,我会认为由于没有指定代理服务器,那么代理端口将被忽略。然而,我看到的几乎每篇关于使用curl访问谷歌地图的帖子都指定了这个端口,但没有指定服务器。这只是复制粘贴污染,还是有其目的? - sootsnoot
你好,我想根据IP地址获取当前位置。有什么方法可以做到这一点吗? - Mr. Tomar
大多数情况下它会返回空响应。可能出了什么问题? - Anjali Patil

37
$address = str_replace(" ", "+", $address);

在使用 file_get_content 之前,请使用上述代码。 也就是说,使用下面的代码

$address = str_replace(" ", "+", $address);

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

它一定会起作用。 因为地址不支持空格,它只支持在空格位置使用+号。


5
请查看内置的 PHP 函数:urlencode() 和 rawurlencode()。 - ekerner
@CodeLღver 我想从地址中获取邮政编码、纬度和经度。 - Manwal
@Manwal 你的意思是邮政编码还是区域代码?我说得对吗? - Code Lღver
@Manwal,您可以通过打印 $json 来检查 API 并获取它返回的数据,希望您能找到所有数据。 - Code Lღver
让我们在聊天中继续这个讨论。点击此处进入聊天室 - Manwal

8
//将urlencode添加到您的地址
$address = urlencode("印度喀拉拉邦特里凡得琅技术园区");
$region = "IND";
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
echo $json; $decoded = json_decode($json); print_r($decoded);
注意:本翻译仅供参考,如需精准翻译请提供更多上下文信息。

网址应该是 http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region - Thomas Orlita
@ThomasOrlita 谢谢。问题是®被更改为不同的字符,所以使用&region代替。 - Sudhir Bastakoti

4

两个注意点:

  • 地址和区域是否URL编码
  • 也许你的电脑运行的代码不允许http访问。尝试加载另一个页面(如'http://www.google.com'),看看是否能正常工作。如果这也不起作用,那么PHP设置可能有问题。

1
$address = str_replace(" ", "+", $address);

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

0

我认为您的 Apache 服务器上已禁用了 allow_url_fopen。您需要将其打开。

请将 allow_url_fopen = 0 更改为 allow_url_fopen = 1

更改后不要忘记重新启动 Apache 服务器。


0

别忘了在代码中加入"$region"才能让它正常运行:

$address = "Salzburg";
$address = str_replace(" ", "+", $address);
$region = "Austria";

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
echo $lat."</br>".$long;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接