Google Earth kml commands

Handles flyto, balloonflyto, and balloon commands for hyper links in kml or html

by Fraser Chapman

HTML

<script src="http://www.google.com/jsapi?fake=.js"></script>
<script src="https://earth-api-utility-library.googlecode.com/svn/trunk/extensions/dist/extensions.pack.js"></script>
<div id="map3d"></div>
<a href="http://www.fraserchapman.co.uk/kml/test2.kml#pm1;balloonFlyTo">pm1 balloonFlyTo</a>

<a href="http://www.fraserchapman.co.uk/kml/test2.kml#pm2;flyTo">pm2 flyTo</a>

<a href="http://www.fraserchapman.co.uk/kml/test2.kml#pm3;balloon">pm3 balloon</a>

<a href="#pm3;balloonFlyTo">pm3 balloonFlyTo (no url)</a>

<a href="http://www.fraserchapman.co.uk/kml/test2.kml#pm3">pm3 (no command)</a>

<a href="http://www.fraserchapman.co.uk/kml/test2.kml#pm3;xx">pm3 (unknown command)</a>

CSS

#map3d {
    width: 400px;
    height: 300px;
}
a {
    display: block
}

JavaScript

var kmlUrl = "http://www.fraserchapman.co.uk/kml/test2.kml"; // test kml

google.load('earth', '1.x', {
    'callback': function () {
        google.earth.createInstance('map3d', function (plugin) {
            plugin.getWindow().setVisibility(true); // display the plugin
            var gex = new GEarthExtensions(plugin); // initialise the extensions 
            gex.util.displayKml(kmlUrl); // load some kml
            kmlCommands.init(gex); // run the kml commands (see below)
        }, function (error) {
            alert(error); // plugin error
        });
    }
});

/*
kmlCommands needs to be passed an instance of GEarthExtensions to work with. e.g.

var gex = new GEarthExtensions(ge);
kmlCommands.init(gex);

kmlCommands also accepts various options allowing one to control the behaviour:

'gex' : GEarthExtensions instance (required)
'element' : DOM element that contains the links you with to modify (default document)
'unsafe' : If true disables content scrubbing (default false)
'ge' : GEPlugin instance (default GEarthExtensions.pluginInstance)

e.g.

// use a specific target
var element = document.getElementById("someElement");
kmlCommands.init('gex': gex, 'element': element); 

// disable content scrubbing
kmlCommands.init('gex': gex, 'unsafe': true); 
*/
;(function (module, window, undefined) {
    var gex = null,
        unsafe = false,
        element = window,
        command = {
            flyto: function (f) {
                this.gex.util.flyToObject(f, {
                    boundsFallback: true
                });
                return f;
            },
            balloon: function (f) {
                var c = this.unsafe ? f.getBalloonHtmlUnsafe() : f.getBalloonHtml(),
                    b = this.gex.pluginInstance.createHtmlStringBalloon('');
                b.setFeature(f);
                b.setContentString(c);
                this.gex.pluginInstance.setBalloon(b);
                return f;
            },
            balloonflyto: function...