google地图网页版_Python使用Google Static Maps API获取指定位置的google地图图像

5ac1e217e5d54328c4b0f86cf8dfde26.png

利用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() 

输出

d570ab9db04374eb362cd122f6fb4008.png

注意:为了检查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:建筑物
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值