页面上显示的 Google 静态地图截图
我有一个用 Python 构建在 Google App Engine 上的网站。其中一项功能是能够使用谷歌静态地图来更新您当前的位置。 Python 代码负责生成静态地图的 URL,而 HTML 模板只需将其放置到位。这就是图像所在空间的样子:
<tr>
<td colspan="4"><img border=0 src="{{img_url}}" /></td>
</tr>
...然后是图像 URL 的生成方式:
self.template_values['img_url'] = "http://maps.google.com/staticmap?center=%s,%s&zoom=%s&size=%s&markers=%s,%s,midreda" % (str(rs['lat']), str(rs['lng']), str(rs['zoom']), str(MAP_SIZE[0])+"x"+str(MAP_SIZE[1]), str(rs['lat']), str(rs['lng']))
rs[] 字典是来自 google 地图搜索的响应。这会生成一个完全有效的静态地图 URL:
<tr>
<td colspan="4"><img border=0 src="http://maps.google.com/staticmap?center=52.955115,-1.149172&zoom=11&size=512x512&markers=52.955115,-1.149172,midreda" /></td>
</tr>
上面的图像 URL 是在搜索“英国诺丁汉”之后得到的。如果您访问该地址,您可以看到它按预期显示、正确的尺寸和一切。但是,在该页面上(您可以在 Spare-wheels.appspot.com/searchlocation 上查看,尽管您需要连接 Facebook 帐户才能使用它),图像并未显示。 HTML 源代码看起来不错:没有明显的编码问题或类似问题。我看不出出了什么问题。有什么想法吗?
谢谢
本
编辑:经过一些测试,我发现地图有时可以工作,但大多数时候不能,并且当它们工作时,它们通常会在刷新后停止工作
I have a site built on the Google App Engine in Python. One of the features is the ability to use google static maps to update your current location. The Python code takes care of generating the URL of the static map and the HTML template simply puts it in place. This is what the space where the image should go looks like:
<tr>
<td colspan="4"><img border=0 src="{{img_url}}" /></td>
</tr>
...and then here's how the image URL is generated:
self.template_values['img_url'] = "http://maps.google.com/staticmap?center=%s,%s&zoom=%s&size=%s&markers=%s,%s,midreda" % (str(rs['lat']), str(rs['lng']), str(rs['zoom']), str(MAP_SIZE[0])+"x"+str(MAP_SIZE[1]), str(rs['lat']), str(rs['lng']))
The rs[] dict is the response from the google maps search. This generates a perfectly valid static map URL:
<tr>
<td colspan="4"><img border=0 src="http://maps.google.com/staticmap?center=52.955115,-1.149172&zoom=11&size=512x512&markers=52.955115,-1.149172,midreda" /></td>
</tr>
The above image URL is after a search for 'Nottingham, UK'. If you visit the address you can see it shows up as intended, correct size and everything. However, on the page (which you can check at spare-wheels.appspot.com/searchlocation, although you need to connect your Facebook account to use it) the image doesn't show up. The HTML source looks fine: no apparent encoding problems or anything like that. I can't see what's gone wrong. Any ideas?
Thanks
Ben
EDIT: After a bit of testing I've found that the maps sometimes work but most of the time they don't, and when they work they generally stop working after a refresh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Google 地图 API v1 已弃用。有关免费静态地图使用不再需要 API 密钥的通知可能仅对使用 Google Maps API V2 的应用程序有效。
包含您的引荐来源网址的浏览器请求会被 403 拒绝。这就是为什么它会在您的浏览器上显示损坏的图像图标。
切换到 v2 URL,或在 URL 中提供 API 密钥。
为了方便起见,Google 提供了升级指南。
您的示例 URL 的正确 v2 URL 是:
注意这些更改:
/staticmap?
=>/maps/api/staticmap?
sensor=false
markers
从52.955115,-1.149172,midreda
更改为 < code>size:mid|color:red|label:A|52.955115,-1.149172有了上述所有内容,地图就可以正确显示红色标记,并且应该确保 Google 将您的请求识别为 V2 请求。
Google Maps API v1 is deprecated. The notice concerning free static map usage no longer needing an API Key is likely only valid to applications using Google Maps API V2.
Browser requests containing your referer are being denied with 403. Hence why it displays the broken image icon on your browser.
Either switch to v2 URL's, or supply an API key in the url.
Google provides an upgrade guide for your convenience.
The correct v2 URL for your sample URL is:
Notice these changes:
/staticmap?
=>/maps/api/staticmap?
sensor=false
markers
from52.955115,-1.149172,midreda
tosize:mid|color:red|label:A|52.955115,-1.149172
With all of the above the map displays correctly for me with the red marker, and should ensure Google recognizes your request as a V2 request.