📜  Python|使用谷歌地理编码 API 计算地点的地理坐标

📅  最后修改于: 2022-05-13 01:55:14.817000             🧑  作者: Mango

Python|使用谷歌地理编码 API 计算地点的地理坐标

地理编码是将地址转换为经纬度等地理坐标的过程,可用于在地图上标记位置。
要使用 Google Maps Geocoding API,必须需要一个 API 密钥,可以在此处获取。需要的模块:

下面是实现:

Python3
# Python program to calculate geographic
# coordinates of places using google
# geocoding API
 
# importing required modules
import requests, json
 
# enter your api key here
api_key = 'Your_api_key'
 
# url variable store url
url = 'https://maps.googleapis.com/maps/api/geocode/json?'
 
# take place as input
place = input()
 
# get method of requests module
# return response object
res_ob = requests.get(url + 'address =' +
                place + '&key =' + api_key)
 
# json method of response object
# convert json format data
# into python format data.
x = res_ob.json()
 
# print the value of x
print(x)


输出 :

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'}