uni farm

pythonでgoogle map apiを使って緯度経度から住所を取得

pythonでgoogle map apiを使って緯度経度から住所を取得

google geocoding apiをつかって 緯度(latitude)、経度(longitude)の座標から住所を取得する

Reverse Geocodingという方法?らしい

環境

  • python: 3.6
  • googlemaps: 3.0.2

api key取得

Get Started  |  Geocoding API  |  Google Developers

から、get startedボタンをクリックして、 apiをenableしていくと、google cloud consoleからapi keyを取得できる

結果取得

httpリクエスト

以下のようなurlでgetすればよい

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY

python client

GitHub - googlemaps/google-maps-services-python: Python client library for Google Maps API Web Servicesパッケージを使用する

pip install -U googlemaps
実装
import googlemaps

key = 'api-key'
gmaps = googlemaps.Client(key=key)

results = gmaps.reverse_geocode((-33.8674869, 151.2069902))

for result in results:
    types = result['types']
    print('types', types)
    addr = result['formatted_address']
    print('addr', addr)

実行すると、タイプ別の粒度で住所が取得できる

types ['locality', 'political']
addr Sydney NSW 2000, Australia
types ['postal_code']
addr Haymarket NSW 2000, Australia
types ['administrative_area_level_2', 'political']
addr Sydney, NSW, Australia
types ['colloquial_area', 'locality', 'political']
addr Sydney NSW, Australia
types ['administrative_area_level_1', 'political']
addr New South Wales, Australia
types ['country', 'political']
addr Australia

参考

2024, Built with Gatsby. This site uses Google Analytics.