Can I trigger a function AFTER a setMap(map) has fully loaded?

132 views
Skip to first unread message
Message has been deleted

chat2joe

unread,
Mar 5, 2011, 3:09:27 PM3/5/11
to Google Maps JavaScript API v3
Hi,

I want to reload a kml file every few seconds with the latest
generated version, this is working fine using setInterval to call the
function below. The function is removing the previous kml then loading
it again, the refresh variable is just to force the page to download
the file again...

function refreshkml ( )
{
kmlLayer.setMap(null);
kmlLayer = new google.maps.KmlLayer('db_output_xml.php?refresh='+
(new Date()).getMilliseconds());
kmlLayer.setMap(map);

}

If the above doesn't format right: http://www.pastie.org/1637107

I know I should share the url but i'm still butchering the code so
it's a bit of a mess.

The problem is, there is a significant delay (few seconds) between the
kml being turned off and the new one loading, during which time there
is no overlay showing. I was thinking of having a second copy of the
kml generating file, toggling between which one is loaded... and
removing the previous one AFTER the new one has fully loaded.

Is there any way to trigger a function when a setMap(map) has fully
loaded to the map?

Thanks for any help or suggested workarounds! ;-)

Joe

Rossko

unread,
Mar 5, 2011, 4:16:05 PM3/5/11
to Google Maps JavaScript API v3
> Is there any way to trigger a function when a setMap(map) has fully
> loaded to the map?

For a kmlLayer, you can listen for metdata_changed
http://groups.google.com/group/google-maps-js-api-v3/search?group=google-maps-js-api-v3&q=metadata_changed

chat2joe

unread,
Mar 5, 2011, 4:44:40 PM3/5/11
to Google Maps JavaScript API v3
I've tried listened for changed zooms and changed defaultviewports...
they work, but on the final map I want to preserve the viewport. Tried
that and the preserved viewport prevents the listener from firing.

Thanks for the reply Rossko, I've tried using that but can't seem to
get it to trigger.

See draft map here: http://lugh2.it.nuigalway.ie/joehyland/2/java_map.html
(excuse the mess)
Click the button to set the refresh function off.

It's flicking between two seperate kml files for now to show the
change better. When complete both will be pointing to the same auto-
generating php/kml file with a random variable on the url.


One of my kml files is "kmlLayer1" so I tried;

google.maps.event.addListener(kmlLayer1, 'metadata_changed',
function() {
alert(toggle);
});

Am I doing that right?

Can you see from the may why it wouldn't trigger? I'm re-assigning the
kml file within the refresh function, so the new random variable gets
attached to the url and is forced to download the new file.



On Mar 5, 9:16 pm, Rossko <ros...@culzean.clara.co.uk> wrote:
> > Is there any way to trigger a function when a setMap(map) has fully
> > loaded to the map?
>
> For a kmlLayer, you can listen for metdata_changedhttp://groups.google.com/group/google-maps-js-api-v3/search?group=goo...

Rossko

unread,
Mar 5, 2011, 8:32:58 PM3/5/11
to Google Maps JavaScript API v3
>   google.maps.event.addListener(kmlLayer1, 'metadata_changed',
> function() {
>         alert(toggle);
>   });
>
> Am I doing that right?

See this post, I think it is relevant
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/ff97009e0ca4f593/e52c3d1d40ed7a11
You're setting up the listener after performing the setMap()

chat2joe

unread,
Mar 5, 2011, 9:30:43 PM3/5/11
to Google Maps JavaScript API v3
I got it working! It works with the listener right after the setMap().

so on the initialization function i put...

setInterval ( "refreshmap()", 7000 );


Then I toggle between the two files in the refresh function;


var toggle = 1;

function refreshmap()
{

if (toggle == 1)
{
kmlLayer1 = new google.maps.KmlLayer('db_output_xml.php?
refresh='+Math.floor(Math.random() * 10000), {preserveViewport:true});
kmlLayer1.setMap(map);

google.maps.event.addListenerOnce(kmlLayer1, 'metadata_changed',
function() {
kmlLayer2.setMap(null);
});

toggle = 2;

}
else
{
kmlLayer2 = new google.maps.KmlLayer('db_output_xml.php?
refresh='+Math.floor(Math.random() * 10000), {preserveViewport:true});
kmlLayer2.setMap(map);


google.maps.event.addListenerOnce(kmlLayer2, 'metadata_changed',
function() {
kmlLayer1.setMap(null);
});

toggle = 1;
}


}






On Mar 6, 1:32 am, Rossko <ros...@culzean.clara.co.uk> wrote:
> >   google.maps.event.addListener(kmlLayer1, 'metadata_changed',
> > function() {
> >         alert(toggle);
> >   });
>
> > Am I doing that right?
>
> See this post, I think it is relevanthttp://groups.google.com/group/google-maps-js-api-v3/browse_thread/th...
Reply all
Reply to author
Forward
0 new messages