0
votes

I'm trying to add a network link when someone clicks on a placemark that has been loaded via KML. What I do is attach an event handler to the globe and check to see if the user clicked on a placemark.

On the html, there is a button that when clicked, removes the network link from google earth when clicked (see trackRemoval). Everything seems to work the first time a placemark is clicked.

The problem is when the placemark is clicked a second time (after having the network link removed), the call to createNetworkLink fails. Attached is the relevant snippets of code.

Can someone see what I'm doing wrong?

var ge = new Array(2);

function clickHandler(event) {
    if (event.getTarget().getType() == 'KmlPlacemark') {
        event.preventDefault();
        var placemark = event.getTarget();
        var device = placemark.getName();
        var networkLink = ge[0].createNetworkLink(device + "link");
        var link = ge[0].createLink("");
        networkLink.setDescription("Vechicle view for" + device);
        networkLink.setName("Track for " + device);
        networkLink.setFlyToView(true);
        link.setHref("http://x.x.x.x/blah/blah.kml");
        link.setRefreshMode(ge[0].REFRESH_ON_INTERVAL);
        link.setRefreshInterval(60);
        networkLink.setLink(link);
        ge[0].getGlobe().getFeatures().appendChild(networkLink);
    }
}

function initgeaor(instance) {
    google.earth.addEventListener(instance.getGlobe(), 'click', clickHandler);
}


function trackRemoval() {
    var device = this.name;
    var networklink = ge[0].getElementById(device + "link");
    ge[0].getGlobe().getFeatures().removeChild(networklink);
}        
1

1 Answers

1
votes

See https://developers.google.com/earth/documentation/reference/interface_kml_object

See the note on the method release(). From my experience this "indeterminate amount of time" is difficult to gauge. So if you remove an object from GE, and then try to add another object with the same id, GE complains and won't create the object - unless that 'indeterminate amount of time' has passed.

You may be able to use a timer after you removeChild() and release() the network link.

You could also give your new KmlNetworkLink a different id each time you create it so there's no collision. I'm not sure if this is an option for you. var networkLink = ge[0].createNetworkLink(device + "link" + idNumber);