php 随机经纬度,使用php,API自动获取经纬度

使用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 "
"; echo $long = $response_a->results[0]->geometry->location->lng;

$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'};

而且它肯定会工作。 由于地址不支持空格,因此它只支持+符号代替空格。

//将urlencode添加到您的地址

$ address = urlencode(“technopark,Trivandrun,Kerala,India”);

$ region =“IND”;

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

echo $ json;

$ decode = json_decode($ json);

的print_r($解码);

两个想法:

地址和区域URL是否被编码 ?

也许你的电脑运行代码不允许http访问。 尝试加载另一个页面(如“ http://www.google.com ”),看看是否有效。 如果这也不起作用,那么PHP设置有问题。

$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'};

…并且不要忘记代码工作的“$ 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."".$long;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值