Deleting Placemarks and Other features

135 views
Skip to first unread message

BobinPhoenix

unread,
Jun 23, 2008, 6:53:23 PM6/23/08
to KML Developer Support - Google Earth Browser Plugin
It seems that this question has been asked before, but I didn't see a
direct answer. I'll ask it in a slightly different way.
Once I say:

var marker = ge.createPlacemark('ID111');

What needs to be done before the above can be executed again without
throwing an exception?

Thanks,

Bob

Paul Rademacher

unread,
Jun 23, 2008, 9:21:51 PM6/23/08
to KML Developer Support - Google Earth Browser Plugin
You won't be able to create a new KML object with a re-used ID until
every reference to the first object is deleted from Earth's object
hierarchy, and also from the browser's Javascript context. A
complication, though, is that you cannot explicitly control
Javascript's garbage collector, and so there is no guarantee of when
an object will actually be fully released from memory.

As a result, the best practice is to not reuse IDs within a session,
but rather always use a new unique ID when creating a new object. Or,
you can always use an empty string for the ID, also.

BobinPhoenix

unread,
Jun 24, 2008, 1:15:20 PM6/24/08
to KML Developer Support - Google Earth Browser Plugin
I don't understand how garbage collection matters. If the plugin has
no knowledge of a placemark how does the frequency of garbage
collection affect it's knowledge of particular placemark's ID.

Taking your remark slightly out of context:

"You won't be able to create a new KML object with a re-used ID until
every reference to the first object is deleted from Earth's object
hierarchy..."

From this I guess my question can be restated as:
How does one delete something from "Earth's object hierarchy"?

Thanks,

Bob

Paul Rademacher

unread,
Jun 24, 2008, 3:39:48 PM6/24/08
to KML Developer Support - Google Earth Browser Plugin
It might be clearer in code:

var placemark = plugin.createPlacemark('id12345');
plugin.getFeatures().appendChild(placemark);
plugin.getFeatures().removeChild(placemark); // Now placemark is
removed from object hierarchy

// But 'placemark' JS variable still exists at this point, so we
can keep using it

plugin.getFeatures().appendChild(placemark); // Add it again
plugin.getFeatures().removeChild(placemark); // Remove it again

// Now delete JS variable
placemark = null;

// We've now removed all references to the placemark (both in JS and
in Earth's features hierarchy)

// BUT, the placemark JS variable won't actually be destryoed until
the JS garbage collector kicks in.
// Technically, the variable still exists (as far as the browser is
concerned) until the garbage
// collector takes it away.

Hope that clarifies things.

- Paul

BobinPhoenix

unread,
Jun 24, 2008, 4:00:36 PM6/24/08
to KML Developer Support - Google Earth Browser Plugin
First, thanks for trying to help.

Second, the interactive examples have been a big
help in having a starting point that also lets me try things easily.

Back to the topic.


ge.createPlacemark('ID333');
log("Created First");
ge.createPlacemark('ID333');
log("Created Second");

The attempt to create the second placemark
throws an exception, even though the first placemark was not assigned
to a variable or made a feature.

While this is trivial example it seems like it would be a memory leak
if there is no way to tell the plugin "I'm done with this".

Thanks,

Bob

Paul Rademacher

unread,
Jun 24, 2008, 6:11:09 PM6/24/08
to KML Developer Support - Google Earth Browser Plugin
Actually, in both cases the browser created a variable which will
eventually be garbage-collected -- you just don't have a handle to the
variables, so they're essentially hidden from your code.
Reply all
Reply to author
Forward
0 new messages