Can I cache the Google Maps API API?

Curious if the Google Maps API v3 javascript caching will be on the local server?

Because sometimes my intranet pages load slowly due to a slow internet connection. Otherwise, it will download the file from the local server and slow down only when executing a map request.

I am even ready to run the cron job to update the javascript file from time to time.

Thanks for any input.

+7
javascript google-maps-api-2
source share
4 answers

Impossible "as is."

When you request a script from Google, they send the headers by script, and these headers contain the no-cache directive.

So, if you want them to be cachable, you have to create a proxy. Instead of pointing the script src to Google, you point it to your server. Then your server makes a call to Google and sends a response to the client.

This way you will control the HTTP header and caching. You can also cache the contents of the script to make fewer connections to Google.

I would not advise anyone to do this in production or on a critical website. All Google APIs are updated frequently and linked more or less together. If something does not sync with something else, it’s hard for you to track the error on your hands.

Hope this helps.

EDIT: I heard that you posted your scripts in the HEAD section of your document. Perhaps this harms your "perceived" page load time. Try moving the script loading just before the </body> and initializing the map in the onload page.

Mike

+13
source share

Starting in 2016, js returns with the heading "Cache-Control: public, max-age = 1800", so it is cached for at least half an hour.

+4
source share

IMHO you cannot cache it. The API script calls objects on a Google server. At best, you can record the results and cache them as images (but then you lose interactivity).

If it is possible to cache GMap results, people will simply cache the entire Google DBMS locally, and I don’t think this is part of the user agreement;).

If you need offline maps, you will have to take non-interactive images or buy a map server.

+3
source share

An option that would not violate Google’s Custom Terms was to save the local OpenStreetMap tile cache using the OpenLayers script to view them. Mostly using free data to create your own map server.

If there are interesting points that are important for your intranet, you can make sure that they are located in OpenStreetMap and configure the render server yourself only with the functions that you need. In the end, overlaying each school in the district over some png will take more work and then just show the png done with the schools in them.

In addition, a slow external connection will require much less if your map server receives / generates tiles during the weekend, and does not constantly hit the Google Maps API.

0
source share

All Articles