簡體   English   中英

使用php、API自動獲取經緯度

[英]Get latitude and longitude automatically using php, API

在我的一個 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 請求失敗! 第 4 行 D:\\Projects\\lon.php 中的 HTTP/1.0 400 錯誤請求

請幫我解決這個問題。

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

它肯定會起作用。 由於地址不支持空格,因此它僅支持用 + 符號代替空格。

//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);

兩個想法:

  • 地址和區域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."</br>".$long;

我認為您的 apache 服務器上的allow_url_fopen已禁用。 你需要打開它。

請將 allow_url_fopen = 0更改為allow_url_fopen = 1

更改后不要忘記重新啟動 Apache 服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM