可以缓存 Google Maps API javascript 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1965236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can the Google Maps API javascript be cached?
提问by Nirmal
Just curious, whether the Google Maps API v3 javascript be cached on the local server?
只是好奇,Google Maps API v3 javascript 是否会缓存在本地服务器上?
Because, sometimes my intranet pages are loading slow because of slower internet connection. Otherwise, it would load the file from local server and slow down only when the map request is made.
因为,有时由于互联网连接速度较慢,我的 Intranet 页面加载速度很慢。否则,它会从本地服务器加载文件,并且只有在发出地图请求时才会减慢速度。
I am even ready to run a cron job to update the javascript file once in a while.
我什至准备好偶尔运行一个 cron 作业来更新 javascript 文件。
Thanks for any input.
感谢您提供任何意见。
回答by Mike Gleason jr Couturier
It's impossible "as is".
“按原样”是不可能的。
When you request the script from Google, they send headers along the script and those headers contains the "no-cache" directive.
当您从 Google 请求脚本时,他们会沿着脚本发送标头,这些标头包含“no-cache”指令。
So if you want them to be cachable, you must create a proxy. Instead of pointing the script src at Google, you point it to your server. Your server then make the call to Google and send the response back to the client.
因此,如果您希望它们可缓存,则必须创建一个代理。不是将脚本 src 指向 Google,而是将其指向您的服务器。然后,您的服务器调用 Google 并将响应发送回客户端。
This way you'll have control over the HTTP header and the caching. You could do caching on the script content as well to make less connections to Google.
这样您就可以控制 HTTP 标头和缓存。您也可以对脚本内容进行缓存,以减少与 Google 的联系。
I wouldn't advise someone to do it on a production or a mission critical website. All Google APIs are updated frequently and are bound more or less together. If something goes out of synch with something else, you have a hard to track bug on your hands.
我不会建议某人在生产或关键任务网站上这样做。所有 Google API 都经常更新,并且或多或少地绑定在一起。如果某些东西与其他东西不同步,你就很难追踪你手上的错误。
Hope that helps.
希望有帮助。
EDIT: I heard you were putting your scripts in the HEADsection of your document. Maybe thats hurting your "perceived" page loading time. Try to move the download of the script just before the </body>tag and the map initialization in the onloadevent of the page.
编辑:我听说您将脚本放在HEAD文档的某个部分。也许这会损害您的“感知”页面加载时间。尝试在页面事件中将脚本的下载移动到</body>标记和地图初始化之前onload。
Mike
麦克风
回答by Andris
As of 2016, the js is returned with "Cache-Control: public, max-age=1800" header, so it is cached for at least for a half hour.
截至 2016 年,js 返回“Cache-Control: public, max-age=1800”标头,因此它至少缓存了半小时。
回答by Traveling Tech Guy
IMHO you can't cache it. The API script calls objects on the Google server. At most, you can capture the results and cache them as images (but then you lose interactivity).
恕我直言,你不能缓存它。API 脚本调用 Google 服务器上的对象。最多,您可以捕获结果并将它们缓存为图像(但随后您会失去交互性)。
If it would be possible to cache GMap results, people would just cache Google's entire DB locally and I don't think this is part of the user agreement ;).
如果可以缓存 GMap 结果,人们只会在本地缓存 Google 的整个数据库,我认为这不是用户协议的一部分;)。
If you want offline maps, you'll have to do with non-interactive images, or buy a map server.
如果您想要离线地图,则必须使用非交互式图像,或者购买地图服务器。
回答by sargas
An option that would not have you violating Google's Terms of Service would be to keep a local cache of OpenStreetMap tiles with the OpenLayers script for viewing them. Basically, using free data to create your own map server.
一个不会让您违反 Google 服务条款的选项是使用 OpenLayers 脚本保留 OpenStreetMap 图块的本地缓存以查看它们。基本上,使用免费数据来创建您自己的地图服务器。
If there are particular points of interest that are important for your intranet, you can make sure that they are in OpenStreetMap and setup a rendering server yourself with just the features you require. After all, overlaying each school in a district over some png's is going to take more work then just showing the png's made with the schools in them.
如果有对您的 Intranet 很重要的特定兴趣点,您可以确保它们在 OpenStreetMap 中,并使用您需要的功能自行设置渲染服务器。毕竟,将一个地区的每所学校覆盖在一些 png 上需要做更多的工作,而不是只显示由其中的学校制作的 png。
It also would take a lot less lag on a slow outside connection if your map server gets/generates the tiles during the weekend instead of hitting the Google Map API all the time.
如果您的地图服务器在周末获取/生成图块而不是一直访问 Google Map API,那么在缓慢的外部连接上也会减少很多延迟。

