php 使用php、API自动获取经纬度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8633574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:08:11  来源:igfitidea点击:

Get latitude and longitude automatically using php, API

phpgoogle-maps-api-3

提问by ramesh

In one of my php applications I have to find out the latitude and longitude of the place from address.

在我的一个 php 应用程序中,我必须从地址中找出该地点的纬度和经度。

I tried this code:

我试过这个代码:

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

But it is showing the following Error :

但它显示以下错误:

Warning: file_get_contents(http://maps.google.com/maps/api/geocode/json?address=technopark, Trivandrun, kerala,India&sensor=false&region=IND) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in D:\Projects\lon.php on line 4

警告:file_get_contents(http://maps.google.com/maps/api/geocode/json?address=technopark, Trivandrun, kerala,India&sensor=false®ion=IND) [function.file-get-contents]: 无法打开流: HTTP 请求失败!第 4 行 D:\Projects\lon.php 中的 HTTP/1.0 400 错误请求

Please help me to solve this issue.

请帮我解决这个问题。

回答by priyank saini

Use curlinstead of file_get_contents:

使用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;

回答by Code L?ver

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

Use the above code before the file_get_content. means, use the following code

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

and it will work surely. As address does not support spaces it supports only + sign in place of space.

它肯定会起作用。由于地址不支持空格,因此它仅支持用 + 符号代替空格。

回答by Sudhir Bastakoti

//add urlencode to your address
$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;

$decoded = json_decode($json);

print_r($decoded);

回答by Max

Two ideas:

两个想法:

  • Are Address and Region URL Encoded?
  • Perhaps your computer running the code doesn't allow http access. Try loading another page (like 'http://www.google.com') and see if that works. If that also doesn't work, then there's something wrong with PHP settings.
  • 地址和区域URL是否编码
  • 也许您运行代码的计算机不允许 http 访问。尝试加载另一个页面(如“ http://www.google.com”),看看是否有效。如果这也不起作用,则 PHP 设置有问题。

回答by shailendra Kumar pathak

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

回答by Deepak Rana

I think allow_url_fopenon your apache server is disabled. you need to trun it on.

我认为您的 apache 服务器上的allow_url_fopen已禁用。你需要打开它。

kindly change allow_url_fopen = 0to allow_url_fopen = 1

请将 allow_url_fopen = 0更改 为allow_url_fopen = 1

Don't forget to restart your Apache server after changing it.

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

回答by bromelio

...and don't forget "$region" for the code to work:

...并且不要忘记“$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;