7

I use google.maps.KmlLayer('http://mywebsite.com/my.kml') to set objects from KML file. It is working, but when I change kml and try to refresh website...I still have the same state as before...without my changes. When I change file name to my2.kml - it is working... Are google caching my kml? What I need to do to update changes with the same kml file name?

3
  • Have you tried to empty the cache of your browser? (Alternatively refreshing the page with CTRL+F5 instead of just F5). Oct 28, 2011 at 14:37
  • Yes I did. Moreover I try to open my site in different browser: Opera, IE, Safari, Chrome, FF...result is the same... Oct 28, 2011 at 15:43
  • @JoachimRohde it won't work. Google is caching the KMLs, not browser at the client.
    – Matyas
    Jan 31, 2017 at 7:25

3 Answers 3

10

The Google servers do in fact cache KML data. Because the Google servers are processing your KML and not your browser, clearing your cache will not help. This is why changing the file name works. In order to prevent caching, add a cache-buster to your KML URL that you create the KML layer with, such as a random number or the current timestamp. http://mywebsite.com/my.kml?rev=323626252346 where the value of rev changes every time you refresh the page would work. You could also write Javascript so that you can click a button that updates the URL on the KML Layer object, removing the need to refresh the page.

7
  • I tried this method but it didn't work. Any other suggestions?
    – Shyam K
    Oct 5, 2012 at 21:10
  • The above is not my question. But here is the sample code. It fetches the KML from the current site and displays it. I think Google caches the KML for almost a day. So it may revert to the actual KML file at any time.
    – Shyam K
    Oct 6, 2012 at 4:55
  • The reverse of the current coordinates is to be shown. Google cached the value :(... So my coordinates point to the water.
    – Shyam K
    Oct 6, 2012 at 5:07
  • I'm sorry. Your method is working. Apparently Google maps takes coordinates from a KML file in the reverse way. It was (x,y) on the KML. When I go to maps.google.com for (x,y) it points to somewhere in Greenland Sea. But when I reversed the coordinates it pointed to the right place. This was not happening earlier(b4 using your method) as the value was getting cached. Thanks for the help. :D
    – Shyam K
    Oct 8, 2012 at 4:47
  • 2
    I'm amazed at how badly this is document in the API.
    – MastaBaba
    Apr 16, 2016 at 15:28
5

Yes, google servers cache the KML data. So avoid this caching, change the kml url to

"http://www.kmlsource.com/foo.kml?dummy=" + (new Date()).getTime();

This will always generate a new website and the caching problem will be solved.

1
  • Great hack. Save my day.
    – tala9999
    Mar 14, 2018 at 14:39
0

I would like to add a strong caveat to the advice to add cache-busting to the KML URLs: please note that if you use a timestamp like (new Date()).getTime(), this means that Google will try to fetch the KML file from your server almost every time a user tries to display your KML layer.

Two consequences:

  • the bandwidth and CPU used by simultaneous file uploads can get very high if you have a peak of visitors on your website (this will of course also depend on the size of your KML);
  • this in turn can add big delays to show the KML on your map.

A better strategy would be to only add a cache-busting parameter when you know the KML has changed. Maybe the browser could receive a hash of your KML file from your server, or a version number of the KML, and add that as an extra parameter to the request. You'd need to recompute the hash, or generate a new version number, every time the KML file got updated.

A lazier, far less efficient idea, would be for the server to generate a new token at regular intervals (e.g. once every 10 minutes, make it a reasonable period for your use-case), and have the browser use that token when it needs to display the KML file.

A much worse idea, but still a little bit better than using a browser-side millisecond timestamp to do cache-busting, is to only change the parameter at most once every 10 minutes on the browser side.

For example, instead of (new Date()).getTime(), you could use something like: Math.floor((new Date()).getTime()/1000/(10*60))*(10*60)

Note that this is a pretty bad solution, because this will still be computed based on the end-user's computer clock. If the end-users have set up incorrect times on their machines, many different timestamps can still be generated within a short period of time.

One of the advantages of using KML layers in Google Maps JavaScript API is that they get cached on Google side: if you don't need that, you might be better served with layers built directly on the browser-side. E.g. use GeoJSON. There is even a doc for that for the Google JS API: https://developers.google.com/maps/documentation/javascript/datalayer#load_geojson

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.