あるphpアプリケーションで、住所からその場所の緯度と経度を調べる必要があります。
次のコードを試してみました:
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
しかし、次のエラーが表示されます:
Warning: 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リクエストに失敗しました!HTTP/1.0 400 Bad Request in D:³³Projects³lon.php on line 4
この問題を解決するために私を助けてください。
file_get_contents
の代わりに curl
を使用します。
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=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®ion=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
とすれば確実に動作します。 アドレスはスペースをサポートしていないため、スペースの代わりに+記号のみをサポートしています。
<pre>; //アドレスにurlencodeを追加します。 $address = urlencode("technopark, Trivandrun, kerala,India"); 地域 = "IND"; json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region");
echo $json;
decoded = json_decode($json);
print_r($decoded); </pre>;
2つのアイデアがある:
アドレスと地域は[URLエンコード][1]されていますか?
おそらくコードを実行しているコンピュータがhttpアクセスを許可していないのでしょう。別のページ('http://www.google.com'など)を読み込んでみて、うまくいくかどうか確認してください。もしそれもうまくいかないようであれば、PHPの設定がおかしいのでしょう。
[1]: http://php.net/manual/en/function.urlencode.php "url encoded";
$address = str_replace(" ", "+", $address);
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$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®ion=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
echo $lat."</br>".$long;