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);
}