2016/01/26

Google Map API で地名から緯度経度を取得する

地名と緯度経度の入力サジェストを作るために、何か良いAPIが無いかを調べてみたところ、GoogleMAPのAPIが使えそうだった。

しかも、API利用のためのキーが必要ない!すばらしい!




print_r(getGoogleGeoCode("東京"));

function getGoogleGeoCode($location, $lang="ja") {
    
    $url = "http://maps.google.com/maps/api/geocode/json?sensor=false";
    $url.= "&language=".$lang;
    $url.= "&address=".urlencode($location);
    $rawdata = file_get_contents($url);
    return json_decode($rawdata,TRUE);
}
第一引数は、地名、第二引数は返ってくる地名の言語(例えば、languageの指定をしないと、入力が東京だとTokyoという結果が返ってきてしまう)

geocode APIで返ってくるデータ

Array
(
    [results] => Array
        (
            [0] => Array
                (
                    [address_components] => Array
                        (
                            [0] => Array
                                (
                                    [long_name] => 東京都
                                    [short_name] => 東京都
                                    [types] => Array
                                        (
                                            [0] => administrative_area_level_1
                                            [1] => political
                                        )

                                )

                            [1] => Array
                                (
                                    [long_name] => 日本
                                    [short_name] => JP
                                    [types] => Array
                                        (
                                            [0] => country
                                            [1] => political
                                        )

                                )

                        )

                    [formatted_address] => 日本, 東京都
                    [geometry] => Array
                        (
                            [bounds] => Array
                                (
                                    [northeast] => Array
                                        (
                                            [lat] => 35.8986468
                                            [lng] => 153.9875217
                                        )

                                    [southwest] => Array
                                        (
                                            [lat] => 24.2242343
                                            [lng] => 138.942758
                                        )

                                )

                            [location] => Array
                                (
                                    [lat] => 35.6894875
                                    [lng] => 139.6917064
                                )

                            [location_type] => APPROXIMATE
                            [viewport] => Array
                                (
                                    [northeast] => Array
                                        (
                                            [lat] => 35.817813
                                            [lng] => 139.910202
                                        )

                                    [southwest] => Array
                                        (
                                            [lat] => 35.528873
                                            [lng] => 139.510574
                                        )

                                )

                        )

                    [place_id] => ChIJ51cu8IcbXWARiRtXIothAS4
                    [types] => Array
                        (
                            [0] => administrative_area_level_1
                            [1] => political
                        )

                )

        )

    [status] => OK
)

サジェストのJavaScriptについてはまた次回。

0 コメント:

コメントを投稿