If you mean being able to turn multiple kmz's on/off at will, ie
treating them as layers, then yes you can.
Say you have 2 kmz files, kmz1 & kmz2, then if you assign each one to
a variable as you fetch them you can turn them on like this.
Fetch/Load:
var currentKmlObject;
var obj_KMZ1;
var obj_KMZ2;
function loadKml(kmlPathUrl) {
google.earth.fetchKml(ge, kmlPathUrl, finishLoadKml);
}
function finishLoadKml(kmlObject) {
// check if the KML was fetched properly
if (kmlObject) {
currentKmlObject = kmlObject;
} else {
alert('Bad KML');
}
}
function fetchKMZ1() {
loadKml(KMZ1_URL);
obj_KMZ1 = currentKmlObject;
}
function fetchKMZ2() {
loadKml(KMZ2_URL);
obj_KMZ2 = currentKmlObject;
}
Then, once fetched, to turn each one on you can use
One thing -- careful with asynchronous callbacks there :) Since
finishLoadKml is called asynchronously, your fetchKMZx functions would
set obj_KMZx to an old currentKmlObject value.
Ideally you'd have two callback functions, one to set obj_KMZ1 and
another to set obj_KMZ2.
Good point Roman ... I did say at a basic level ;)
However that does bring up something it would be nice to be able to do
though.
Suppose in the example above you had a list of KMZs you wanted to load
one after the other but didn't know before hand how many you had - how
would you go about using the same LoadKML function (+callback) for all
of them.
In other words what would be the best way to only move to the next in
the list when the callback had finished. Would using some form of
switch, KMLLoaded (true/false) say, that is turned off in the Load
function and turned back on in the callback with a Do While KMLLoaded
= false ..wait ... loop controlling the move through the list be a
solution.
I suspect there would be an elegant solution to this and would be
interested in knowing what it is as I suspect there would be many
cases where you'd want to do something to KML once, any only when,
it's finished loading.
> Good point Roman ... I did say at a basic level ;)
> However that does bring up something it would be nice to be able to do
> though.
> Suppose in the example above you had a list of KMZs you wanted to load
> one after the other but didn't know before hand how many you had - how
> would you go about using the same LoadKML function (+callback) for all
> of them.
> In other words what would be the best way to only move to the next in
> the list when the callback had finished. Would using some form of
> switch, KMLLoaded (true/false) say, that is turned off in the Load
> function and turned back on in the callback with a Do While KMLLoaded
> = false ..wait ... loop controlling the move through the list be a
> solution.
> I suspect there would be an elegant solution to this and would be
> interested in knowing what it is as I suspect there would be many
> cases where you'd want to do something to KML once, any only when,
> it's finished loading.
Just kick off the next load from your completion function.
something like (untested):
var kmlFiles = [<list of urls>];
var kmlObjects = [];
var kmlFileIx = 0;
function loadNextKml()
{
if (kmlFileIx >= kmlFiles.length) return;
window.google.earth.fetchKml(ge,kmlFiles[kmlFileIx], function(kml) {
kmlObjects[kmlFileIx++] = kml;
// other stuff
loadNextKml();
});
}
But there's no real need to force them to be sequential. You could
kick off all the loads, and then handle the completions in whatever
order they arrive.
var kmlFiles = [<list of urls>];
var kmlObjects = [];
You could then show/hide the objects in the g_kmls array in your UI
(g_kmls[i].obj is a KmlObject and g_kmls[i].url is the URL). One idea
is to disable UI and then enable it again at the end of the callback.
> Good point Roman ... I did say at a basic level ;)
> However that does bring up something it would be nice to be able to do
> though.
> Suppose in the example above you had a list of KMZs you wanted to load
> one after the other but didn't know before hand how many you had - how
> would you go about using the same LoadKML function (+callback) for all
> of them.
> In other words what would be the best way to only move to the next in
> the list when the callback had finished. Would using some form of
> switch, KMLLoaded (true/false) say, that is turned off in the Load
> function and turned back on in the callback with a Do While KMLLoaded
> = false ..wait ... loop controlling the move through the list be a
> solution.
> I suspect there would be an elegant solution to this and would be
> interested in knowing what it is as I suspect there would be many
> cases where you'd want to do something to KML once, any only when,
> it's finished loading.
This process seems like it should be working since ge.getFeatures
().hasChildNodes() returns false before the functions are called and
it returns true afterward. However, I don't see the objects appear
onto the map. If I use the exact URL that I'm sending the function to
load the KMZ file into Google Earth itself, it works. I tried setting
visibility to true just to check but had no luck. The URL to the KMZ
file that I am using to test this process with is
http://kml.strategiclink.com/layers/Bridges/bridges.kmz. Any ideas?
> This process seems like it should be working since ge.getFeatures
> ().hasChildNodes() returns false before the functions are called and
> it returns true afterward. However, I don't see the objects appear
> onto the map. If I use the exact URL that I'm sending the function to
> load the KMZ file into Google Earth itself, it works. I tried setting
> visibility to true just to check but had no luck. The URL to the KMZ
> file that I am using to test this process with ishttp://kml.strategiclink.com/layers/Bridges/bridges.kmz. Any ideas?