3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GeopyでGoogle MapのAPIを叩く方法

Posted at

概要

知人にGeopyについて聞かれて調べたときの備忘録。
いつか自分でgeocodingするときのために。
(geocodingとは、住所↔緯度経度の変換のことを指すらしい)

Geopyのインストール

環境はAnaconda (windows 10)上で構築。

pip install geopy

GCPのAPI Keyを取得

Google Maps PlatformからAPI Keyを取得。

住所から座標へ

正確な住所から座標への変換ができれば十分だけど、Google mapと同じように、適当な文字列からいい感じの検索結果を出してくれることを期待。。。

from geopy.geocoders import GoogleV3
g = GoogleV3(API_KEY) 
g.geocode('白旗の湯', timeout=10)
Location(112-1 Kusatsu, Agatsuma District, Gunma 377-1711, Japan, (36.6225756, 138.5962787, 0.0))

Google Mapで群馬県吾妻郡草津町112-1を確認してみる。
無事、白旗の湯に辿り着きました。すごい。

kusatsu.png

フル住所でも一応確認。

g.geocode('群馬県吾妻郡草津町112-1', timeout=10)
Location(112-1 Kusatsu, Agatsuma District, Gunma 377-1711, Japan, (36.622506, 138.596808, 0.0))

座標から住所へ

逆方向もついでに。

g.reverse((36.6225756, 138.5962787, 0.0), timeout=10)
[Location(112-1 Kusatsu, Agatsuma District, Gunma 377-1711, Japan, (36.6225756, 138.5962787, 0.0)),
 Location(419 Kusatsu, Agatsuma-gun, Gunma 377-1711, Japan, (36.6226335, 138.5961949, 0.0)),
 Location(Yutaki Street, Kusatsu, Agatsuma-gun, Gunma 377-1711, Japan, (36.6224974, 138.596249, 0.0)),
 Location(377-1711, Japan, (36.6271446, 138.5887875, 0.0)),
 Location(Kusatsu, Agatsuma District, Gunma 377-1711, Japan, (36.6271446, 138.5887875, 0.0)),
 Location(Kusatsu, Agatsuma District, Gunma, Japan, (36.6207181, 138.5960945, 0.0)),
 Location(Agatsuma District, Gunma, Japan, (36.5314431, 138.6388879, 0.0)),
 Location(Gunma, Japan, (36.5605388, 138.8799972, 0.0)),
 Location(Honshu, Japan, (36.16999990000001, 138.04, 0.0)),
 Location(Japan, (36.204824, 138.252924, 0.0))]

ちゃんと白旗の湯に行けている。
行きたいなあ。。。

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?