Mauno Vähä
Mauno Vähä

Reputation: 9788

Google maps v3: how to force reloading of tiles

I have come accross pretty annoying problem. I started to test my google maps code with slower internet connection and it doesnt pretty much work anymore like it suppose to.

The problem is that I am listening "tilesloaded" event when Im doing animations on map BUT because connection is slow some tiles might hang and doesnt load.. there will be gray tile middle of the map and my program wont continue because it waits it to be loaded.

I used firebug console to catch error and it said:

Image corrupt or truncated: https://mts0.googleapis.com/vt?lyrs=m@183000000&src=apiv3&hl=fi-FI&x=18706&y=8527&z=15&s=Galil&style=api%7Csmartmaps

I pasted that to browser and found the missing tile image from there. darn it! It exists but sometimes doesnt load :)

So the question is that how I can force reloading of missing tiles or all tiles after some period of time, as far as I know there is no such event which will be triggered if some tile cant be loaded? and it seems that error which appeared to console are not given always. I also tried using "idle" event of google maps but it doesnt wait tiles to be loaded and will make program continue as laggy. (not someting what I want.)

I am also using google.maps.event.trigger(my_map, 'resize'); but it doenst seem to help.

Edit:

Refreshing full page is not an option because I would lose values of my variables etc. and this slow loading is hard to test but occurs when panning to some specific point on map which is so far that tiles needs to be reloaded.

Any ideas? Thnx in advance!

Upvotes: 1

Views: 7079

Answers (4)

Stephen Paul
Stephen Paul

Reputation: 39025

Yes you absolutely can using map.setZoom(0). I use this function to force my tiles to redraw when I'm fitting to new bounds:

export function setBounds(bounds) { map.setZoom(0); // forces tiles to invalidate google.maps.event.addListenerOnce(map, 'idle', () => map.fitBounds(bounds)); }

I'm doing this because I don't like the smooth zooming effect that the library uses by default.

Upvotes: 0

Dawid D
Dawid D

Reputation: 1151

in javascript map responds on browser resize, and loads missing tiles, so all what you need is fire browser event "resize"

var evt = document.createEvent('UIEvents');
evt.initUIEvent('resize', true, false,window,0);
window.dispatchEvent(evt);

Upvotes: 3

Chris Broadfoot
Chris Broadfoot

Reputation: 5112

First of all, this sounds like a bug (the API should retry fetching tiles if they time out). Please file that over on the issue tracker.

You can probably trigger a refresh by notifying the map of a change to the mapTypeId property.

google.maps.event.notify(map, 'mapTypeId')

Upvotes: 0

Marcelo
Marcelo

Reputation: 9407

There is no access to the base maps in the V3 API, so all you can do is to destroy your map object (map = null) and create a new identical one. The tiles that were successfully downloaded should still be in the browser's cache.

Upvotes: 2

Related Questions