页面上显示的 Google 静态地图截图

发布于 2024-10-24 09:04:41 字数 1163 浏览 0 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2024-10-31 09:04:41

Google 地图 API v1 已弃用。有关免费静态地图使用不再需要 API 密钥的通知可能仅对使用 Google Maps API V2 的应用程序有效。

包含您的引荐来源网址的浏览器请求会被 403 拒绝。这就是为什么它会在您的浏览器上显示损坏的图像图标。

切换到 v2 URL,或在 URL 中提供 API 密钥。

为了方便起见,Google 提供了升级指南

您的示例 URL 的正确 v2 URL 是:

http://maps.google.com/maps/api/staticmap?center=52.955115,-1.149172&zoom=11&size=512x512&markers=size:mid|color:red|label:A|52.955115,-1.149172&sensor=false

注意这些更改:

  • 已更改 /staticmap? => /maps/api/staticmap?
  • 添加了 sensor=false
  • markers52.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:

http://maps.google.com/maps/api/staticmap?center=52.955115,-1.149172&zoom=11&size=512x512&markers=size:mid|color:red|label:A|52.955115,-1.149172&sensor=false

Notice these changes:

  • Changed /staticmap? => /maps/api/staticmap?
  • Added sensor=false
  • Changed markers from 52.955115,-1.149172,midreda to size: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文