Adding and removing kmz files as layers

1,846 views
Skip to first unread message

SteveAtSLC

unread,
Jan 5, 2009, 10:51:38 AM1/5/09
to KML Developer Support - Google Earth Browser Plugin
How can I add and remove kmz files as layers? Are there any existing
examples with the google earth api?
Message has been deleted

Nymor

unread,
Jan 5, 2009, 1:02:29 PM1/5/09
to KML Developer Support - Google Earth Browser Plugin
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

ge.getFeatures().appendChild(obj_KMZ1);
and/or
ge.getFeatures().appendChild(obj_KMZ2);

and to switch them off use

ge.getFeatures().removeChild(obj_KMZ1);
and/or
ge.getFeatures().removeChild(obj_KMZ2);

If you only want to change their visibility, as opposed to append/
remove, you could also use

obj_KMZ1.setVisibility(true);
obj_KMZ2.setVisibility(true);

and

obj_KMZ1.setVisibility(false);
obj_KMZ2.setVisibility(false);

... to do that however ensure that each has been appended first.

I haven't checked the above but I use a variation of it so it should
work at the basic level.

Hope that helps

Regards
Nymor

Roman N

unread,
Jan 5, 2009, 1:21:58 PM1/5/09
to KML Developer Support - Google Earth Browser Plugin
Nymor,

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.

- Roman

Nymor

unread,
Jan 5, 2009, 1:45:36 PM1/5/09
to KML Developer Support - Google Earth Browser Plugin
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.

Regrads
Nymor

Markw65

unread,
Jan 5, 2009, 3:02:12 PM1/5/09
to KML Developer Support - Google Earth Browser Plugin
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 = [];

function loadKmlFile(ix)
{
window.google.earth.fetchKml(ge, kmlFiles[ix], function(kml) {
kmlObjects[ix] = kml;
//other stuff
});
}

for (var i=0; i< kmlFiles.length;i++) {
loadKmlFile(i);
}

Roman N

unread,
Jan 5, 2009, 3:02:53 PM1/5/09
to KML Developer Support - Google Earth Browser Plugin
You could use JavaScript closures (anonymous functions) to construct
an elegant solution. Here's a basic idea:

var g_kmls = [];

function loadKml(url) {
var kmlData = { url: url, obj: null };
g_kmls.push(kmlData);

google.earth.fetchKml(ge, url, function(kmlObject) {
if (kmlObject)
kmlData.obj = kmlObject;
else
alert('error loading ' + url);
});
}

loadKml('http://path/to/1.kml');
loadKml('http://path/to/2.kml');
loadKml('http://path/to/3.kml');

...

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.

- Roman

SteveAtSLC

unread,
Jan 8, 2009, 9:44:58 AM1/8/09
to KML Developer Support - Google Earth Browser Plugin
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?

On Jan 5, 1:02 pm, Nymor wrote:

SteveAtSLC

unread,
Jan 8, 2009, 10:04:16 AM1/8/09
to KML Developer Support - Google Earth Browser Plugin
I am having this same issue when loading the KML/KMZ with a network
link.

SteveAtSLC

unread,
Jan 8, 2009, 10:23:50 AM1/8/09
to KML Developer Support - Google Earth Browser Plugin
I solved my issue. Thanks for all of the help guys
Reply all
Reply to author
Forward
0 new messages