1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > php 随机经纬度 使用php API自动获取经纬度

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

时间:2020-03-08 09:54:03

相关推荐

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

使用curl而不是file_get_contents :

$address = "India+Panchkula"; $url = "/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("/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(“/maps/api/geocode/json?address=$address&sensor=false&region=$region”);

echo $ json;

$ decode = json_decode($ json);

的print_r($解码);

两个想法:

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

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

$address = str_replace(" ", "+", $address); $json = file_get_contents("/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("/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;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。