python 获取经纬度
时间: 2023-10-18 13:15:05 浏览: 48
要获取经纬度,您需要使用地理编码API,例如Google Maps API或百度地图API。以下是使用Google Maps API的Python代码示例:
```python
import requests
address = "1600 Amphitheatre Parkway, Mountain View, CA" # 输入您要查询的地址
url = "https://maps.googleapis.com/maps/api/geocode/json"
params = {
"key": "YOUR_API_KEY", # 将YOUR_API_KEY替换为您的Google Maps API密钥
"address": address
}
response = requests.get(url, params=params)
if response.status_code == 200:
results = response.json()["results"]
if len(results) > 0:
location = results[0]["geometry"]["location"]
lat = location["lat"]
lng = location["lng"]
print(f"经度: {lng}, 纬度: {lat}")
else:
print("请求失败")
```
请注意,您需要将“YOUR_API_KEY”替换为您的Google Maps API密钥。您可以在Google Cloud Console中创建一个API密钥。