グーグル マップ作成の為GEOコード取得

下記のようなマップ作成の為に、Google Geocoding APIを使い、それぞれのロケーションを、ジオコード(経度、緯度)で取得したので、忘れる前にメモりました。

米国 カリフォルニア州 日系企業 地図 フルスクリーンサイズ

ワードプレスのHTTP API を利用して、Google Geocoding API リクエストして、JSON を取得。

取得したデータをそのまま json_decode すると、エラー(Warning: json_decode() expects parameter 1 to be string)が発生する為、json_decode( $response[“body”])とした。

$response = wp_remote_get("https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY");
$responsebody = json_decode( $response["body"],true);
print( $responsebody["results"][0]["geometry"]["location"]["lat"]);
print( $responsebody["results"][0]["geometry"]["location"]["lng"]);

後はループを回して、必要分を取得

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です