
利用Google Static Maps API,可以在网页上嵌入Google Maps图片,而无需JavaScript或任何动态页面加载。Google Static Maps API服务基于通过标准HTTP请求发送的URL参数创建地图,并将该地图返回为可以显示在网页上的图像。
要使用此服务,您需要一个API密钥,请在此处获取它(https://developers.google.com/maps/documentation/static-maps/intro)。
注意:需要在google上创建付费帐户,然后才能使用googleapi。
所需模块:
import requests
下面是实现:
# Python program to get a google map
# image of specified location using
# Google Static Maps API
# importing required modules
import requests
# Enter your api key here
api_key = "_your_api_key_"
# url variable store url
url = "https://maps.googleapis.com/maps/api/staticmap?"
# center defines the center of the map,
# equidistant from all edges of the map.
center = "Dehradun"
# zoom defines the zoom
# level of the map
zoom = 10
# get method of requests module
# return response object
r = requests.get(url + "center =" + center + "&zoom =" +
str(zoom) + "&size = 400x400&key =" +
api_key + "sensor = false")
# wb mode is stand for write binary mode
f = open('address of the file location ', 'wb')
# r.content gives content,
# in this case gives image
f.write(r.content)
# close method of file object
# save and close the file
f.close()
输出:

注意:为了检查API密钥是否正常工作,尽管将r.content保存为.png文件,也将其存储在.txt文件中。如果API密钥无效,API将返回此错误消息,而不是图像“The Google Maps API server rejected your request. The provided API key is invalid“。
以下列表显示了每个缩放级别可以看到的大致细节级别:
1:世界
5:陆地/洲
10:城市
15:街道
20:建筑物