使用 Python 中的 Google 地理編碼 API 計算地點的地理座標?


要獲取地點的地理座標(即經度和緯度),我們可以使用谷歌地圖地理編碼 API。

要求

要獲取地點的座標,我們需要使用地理編碼 API,可透過以下連結獲取

https://developers.google.com/maps/documentation/geocoding/get-api-key

除了 api_key 之外,我們將使用 python

  • 請求模組(提取座標)
  • Json 模組(用於轉換)。

以下是實現上述功能的程式

# Import required library
import requests
import json
#Enter the place name
place = input("Please enter place name: ")
#Place your google map API_KEY to a variable
apiKey = 'YOUR_API_KEY'
#Store google geocoding api url in a variable
url = 'https://maps.googleapis.com/maps/api/geocode/json?'
# call get method of request module and store respose object
r = requests.get(url + 'address =' + place + '&key =' + apiKey)
#Get json format result from the above response object
res = r.json()
#print the value of res
print(res)

輸出

Please enter place name: Dehradun
{'results': [{'address_components': [{'long_name': 'Dehradun', 'short_name': 'Dehradun', 'types': ['locality', 'political']}, {'long_name': 'Dehradun', 'short_name': 'Dehradun', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Uttarakhand', 'short_name': 'UK', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'India', 'short_name': 'IN', 'types': ['country', 'political']}], 'formatted_address': 'Dehradun, Uttarakhand, India', 'geometry': {'bounds': {'northeast': {'lat': 30.4041936, 'lng': 78.1089305}, 'southwest': {'lat': 30.2466633, 'lng': 77.92533879999999}}, 'location': {'lat': 30.3164945, 'lng': 78.03219179999999}, 'location_type': 'APPROXIMATE', 'viewport': {'northeast': {'lat': 30.4041936, 'lng': 78.1089305}, 'southwest': {'lat': 30.2466633, 'lng': 77.92533879999999}}}, 'place_id': 'ChIJr4jIVsMpCTkRmYdRMsBiNUw', 'types': ['locality', 'political']}], 'status': 'OK'}

更新於:2019-07-30

206 次瀏覽

開啟你的職業生涯

完成課程並獲得認證

開始使用
廣告