LoginSignup
6
6

More than 5 years have passed since last update.

Yahoo!リバースジオコーダAPI使ってみた

Last updated at Posted at 2015-07-31

Yahoo!リバースジオコーダAPIは、緯度・経度で指定された場所の住所検索結果を返してくれるWebAPI

APIを使うまで

Yahoo IDを持っているとする

次のURLからアプリーケーション登録をして,アプリーケーションIDを発行してもらう
https://e.developer.yahoo.co.jp/register

実際に使ってみる

サンプルコード

アプリケーションIDを別ファイルでdefineして使いたいので,以下のようなアプリケーションID設定のためのphpファイルを作る

<?php
define("appid", "自分のアプリケーションID");
?>

実際に,リバースジオコーダを使って緯度経度から,住所に変換しているphpファイル

<?php

require_once("appid.php");

$lat = 35.68381981;//緯度
$lon = 139.77456498;//経度

$uri = "http://reverse.search.olp.yahooapis.jp/OpenLocalPlatform/V1/reverseGeoCoder"
    . "?lat=" . $lat . "&lon=" . $lon . "&appid=" . appid . "&output=json";

$curl = curl_init($uri);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($curl);
curl_close($curl);

$res = json_decode($response,true);

$locationName = $res[Feature][0][Property][Address];

print $locationName."\n";
?>

実行結果

$php lonlatToStreetAdress.php
東京都中央区日本橋1丁目

メモ

curl

Httpで情報を取得する際に使用する
http://php.net/manual/ja/function.curl-init.php
上のサンプルコードでは

$uri = "http://reverse.search.olp.yahooapis.jp/OpenLocalPlatform/V1/reverseGeoCoder"
    . "?lat=" . $lat . "&lon=" . $lon . "&appid=" . appid . "&output=json";

$curl = curl_init($uri);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($curl);
curl_close($curl);

で使っている

curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);

は転送用のオプション
http://php.net/manual/ja/function.curl-setopt.php
'CURLOPT_RETURNTRANSFER'は返り値を文字列として返してくれる
第3引数は,第2引数のオプションをtureにするかfalseにするかを指定する

json_decode

json文字列をデコードする
http://php.net/manual/ja/function.json-decode.php

$res = json_decode($response,true);

第2引数をTRUEにすると,返り値が連想配列となる

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6