首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用php,API自动获取纬度和经度

使用php,API自动获取纬度和经度
EN

Stack Overflow用户
提问于 2011-12-26 06:47:27
回答 6查看 112K关注 0票数 24

在我的php应用程序中,我必须从地址中了解这个地方的纬度和经度。

我试过这个代码:

代码语言:javascript
运行
复制
$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®ion=IND)函数。文件-获取内容:未能打开流: HTTP请求失败!第4行D:\Projects\lon.php中的HTTP/1.0 400坏请求

请帮我解决这个问题。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2012-07-09 13:00:29

使用curl而不是file_get_contents

代码语言:javascript
运行
复制
$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;
票数 40
EN

Stack Overflow用户

发布于 2011-12-26 06:56:11

代码语言:javascript
运行
复制
$address = str_replace(" ", "+", $address);

在file_get_content之前使用上面的代码。方法,使用以下代码

代码语言:javascript
运行
复制
$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'};

这肯定会奏效的。由于address不支持空格,所以它只支持+符号而不是空格。

票数 37
EN

Stack Overflow用户

发布于 2011-12-26 06:51:59

有两个想法:

  • 是地址和区域URL编码
  • 可能您的计算机运行代码不允许http访问。尝试加载另一个页面(比如'http://www.google.com'),看看它是否有效。如果这也不起作用,那么PHP设置就有问题了。
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8633574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档