[GIS] n event fired when a kml layer has finished loading in google maps

google mapskml

Is there an event fired when a kml layer has finished loading in google maps?

Google maps api version 3

To add a layer I have code like:

var layer = new google.maps.KmlLayer(layerUrl, { map: map, preserveViewport : true });

To remove a layer, I have code like:

layer.setMap(null);
layer = null;

The kml are rendered by google maps as images on the clients machine (i.e. the polygons are not created on the client).

I have a number of kml files displayed on a map. When the user clicks a button, I remove those KML layers, and replace them with other similar KML layers (depending on user input).

The transition is a bit slow. The user sees the map without any KML layer for about 30 seconds.

I would like to display a loading sign, from the moment the user clicks the button, to the time that the KML layer finally appears. I need some way to decide when to remove the loading sign.

Thanks

edit:

Looking at stackoverflow (I should have checked), they suggest using:

google.maps.event.addListener(layer, 'metadata_changed', function () {

e.g. here:

https://stackoverflow.com/questions/5668816/google-maps-api-3-loading-icon

On my first draft, that event seems to fire some time before the layer is actually displayed, but I shall investigate further.

Best Answer

If you have a large number of KML features to display, metadata_changed will fire before the features are displayed, because metadata_changed is fired after the information about the Kml Layer is loaded, but there may be a delay before many features are actually rendered on the client. The lag is due to hardware/software on the client, so I don't think you can get any better than metadata_changed.