Python3でgooglemapsを使ってみる1

触りはじめのため、とりあえずAPIKEYの取得と情報の取得のみ。

[amazonjs asin="B00OJVMK5O" locale="JP" title="How Google Works"]

使うモジュール

googlemaps/google-maps-services-python

 

前提

python3をインストールしてあること。

 

インストール

pyvenvで仮想環境を構築してpipをアップグレードしておく。

[shell]
$ mkdir ~/python
$ pyvenv ~/python/maps_python
$ source ~/python/maps_python/bin/activate
(maps_python) $ pip install –upgrade pip
[/shell]

googlemapsをインストールする。
ipythonもインストールしておく。

[shell]
(maps_python) $ pip install googlemaps
(maps_python) $ pip install ipython

[/shell]

API KEYの取得

https://console.developers.google.com//flows/enableapi?apiid=geocoding_backend&keyType=SERVER_SIDE
にアクセスして、チェックボックスにチェックし、同意して続行をクリック。

スクリーンショット 2015-08-01 3.10.33

 

プロジェクトの作成が終わったら認証情報に進むボタンをクリック。

スクリーンショット 2015-08-01 3.32.05

 

画面が遷移したら、作成ボタンをクリック。

スクリーンショット 2015-08-01 3.11.44

 

APIキーを控えておく。

スクリーンショット 2015-08-01 3.11.57

 

ipythonでgooglemapsを使ってみる。
東中野の情報を取得。

[shell]
(maps_python) $ ipython
^[[A^[[A^[[APython 3.4.3 (default, Jun 1 2015, 09:58:35)
Type "copyright", "credits" or "license" for more information.

IPython 3.2.1 — An enhanced Interactive Python.
? -> Introduction and overview of IPython’s features.
%quickref -> Quick reference.
help -> Python’s own help system.
object? -> Details about 'object’, use 'object??’ for extra details.

In [1]: import googlemaps

In [2]: gmaps = googlemaps.Client(key="上で取得したAPIキー")

In [3]: geocode_result = gmaps.geocode("東中野")

In [4]: geocode_result
Out[4]:
[{'address_components’: [{'long_name’: 'Higashinakano Station’,
'short_name’: 'Higashinakano Station’,
'types’: ['train_station’,
'transit_station’,
'point_of_interest’,
'establishment’]},
{'long_name’: '8’,
'short_name’: '8’,
'types’: ['sublocality_level_3’, 'sublocality’, 'political’]},
{'long_name’: '3 Chome’,
'short_name’: '3 Chome’,
'types’: ['sublocality_level_2’, 'sublocality’, 'political’]},
{'long_name’: 'Higashinakano’,
'short_name’: 'Higashinakano’,
'types’: ['sublocality_level_1’, 'sublocality’, 'political’]},
{'long_name’: 'Nakano-ku’,
'short_name’: 'Nakano-ku’,
'types’: ['locality’, 'political’]},
{'long_name’: 'Tōkyō-to’,
'short_name’: 'Tōkyō-to’,
'types’: ['administrative_area_level_1’, 'political’]},
{'long_name’: 'Japan’,
'short_name’: 'JP’,
'types’: ['country’, 'political’]},
{'long_name’: '164-0003’,
'short_name’: '164-0003’,
'types’: ['postal_code’]}],
'formatted_address’: 'Higashinakano Station, 3 Chome-8 Higashinakano, Nakano-ku, Tōkyō-to 164-0003, Japan’,
'geometry’: {'location’: {'lat’: 35.706827, 'lng’: 139.683029},
'location_type’: 'APPROXIMATE’,
'viewport’: {'northeast’: {'lat’: 35.70817598029149,
'lng’: 139.6843779802915},
'southwest’: {'lat’: 35.7054780197085, 'lng’: 139.6816800197085}}},
'place_id’: 'ChIJSR8vfMnyGGARp8bZabbEJCU’,
'types’: ['train_station’,
'transit_station’,
'point_of_interest’,
'establishment’]}]

[/shell]

取得できたー。