Use JavaScript inside balloons in the Google Earth client - kml

I've found a resource suggesting there is a method do this, however the referenced links are throwing google code errors. Does anyone have any examples of using javascript in balloons?
I have a few examples that I have made:
<Placemark>
<name>Object</name>
<description><![CDATA[<br><br><br>
<input value="Test" onchange="this.value=this.value.toUpperCase()">
]]></description>
<gx:balloonVisibility>1</gx:balloonVisibility>
<Polygon>
<extrude>1</extrude>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-1.278059,53.020596,0 -1.278079,53.02062700000001,0 -1.278115,53.02065200000001,0 -1.278164,53.020667,0 -1.278219,53.02067,0 -1.278272,53.020662,0 -1.278316,53.020642,0 -1.278345,53.02061400000001,0 -1.278356,53.020582,0 -1.278346,53.020549,0 -1.278318,53.020521,0 -1.278274,53.020501,0 -1.278222,53.020491,0 -1.278167,53.020494,0 -1.278118,53.020508,0 -1.27808,53.020533,0 -1.27806,53.020563,0 -1.278059,53.020596,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
However none of the examples I have been able to make are very complex. And I can't figure out how to 'explore' it either... As there are no developer tools...

I have found some information here:
JavaScript In KML Ignored By Google Earth Plugin
Code:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Folder>
<name>South Florida</name>
<open>1</open>
<Document>
<name>Miami</name>
<Style id="miami_style">
<IconStyle>
<Icon>
<href>http://i.imgur.com/CNrRU.gif</href>
</Icon>
</IconStyle>
<BalloonStyle>
<text><![CDATA[<font face="Arial">$[description]</font>]]></text>
</BalloonStyle>
</Style>
<Folder>
<name>Miami</name>
<open>1</open>
<Placemark id="Miami">
<name>Miami</name>
<description><![CDATA[
<script type="text/javascript">
function hideImage() {
var image = document.getElementById("image");
image.style.opacity = 0;
image.style.MozOpacity = 0;
image.style.KhtmlOpacity = 0;
image.filter = "alpha(opacity=0)";
}
</script>
<button id='clicker' onclick='hideImage();'>Click Me</button>
<img id="image" src="http://i.imgur.com/4rhT7.png">
]]></description>
<styleUrl>#miami_style</styleUrl>
<Point>
<coordinates>-80.22643611111111,25.788952777777777,0</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</Folder>
</kml>
Ultimately it is bog standard HTML. There are limitations though.
alert() doesn't work - all messages have to be written to elements.
VBS and Other scripting languages don't work. (Because it's not an IE control)
It's very limited in scope - it not see / interact with neighboring placemarks.
Unable to get window object
Can't appear to create objectURLs

Related

Managing a KML Linestring properties

I am plotting a LineString. For some reason, I don't seem to effect the LineStyle. My code looks identical to many examples but no matter what color or width I place in the LineStyle, it always comes out as a thick blue line.
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Qtr Min Grid Maker</name>
<LookAt>
<longitude>-121.5</longitude>
<latitude>38</latitude>
<altitude>0</altitude>
<range>740933.8825924395</range>
<tilt>0</tilt>
<heading>0</heading>
</LookAt>
<Folder>
<name>Grids</name>
<Style id="linestyle1">
<LineStyle>
<color>7f0000ff</color>
<width>1</width>
<gx:labelVisibility>1</gx:labelVisibility>
</LineStyle>
</Style>
<Placemark>
<name>QTR</name>
<visibility>0</visibility>
<open>1</open>
<styleUrl>#linestyle1</styleUrl>
<LineString>
<coordinates>
-124.75,40,0
-124.5,40,0
-124.25,40,0
-124,40,0
-123.75,40,0
-123.5,40,0
-123.25,40,0
-123,40,0
</coordinates>
</LineString>
</Placemark>
</Folder>
</Document>
</kml>
It works for me if I move the shared styles to the top level (inside the <Document> tag):
example
From the documentation (see the description of <StyleSelector>):
A style defined within a Feature is called an "inline style" and
applies only to the Feature that contains it. A style defined as the
child of a <Document> is called a "shared style." A shared style must
have an id defined for it. This id is referenced by one or more
Features within the <Document>.
Yours is neither a child of <Document> nor within a <Placemark>
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="linestyle1">
<LineStyle>
<color>7f0000ff</color>
<width>1</width>
<gx:labelVisibility>1</gx:labelVisibility>
</LineStyle>
</Style>
<name>Qtr Min Grid Maker</name>
<LookAt>
<longitude>-121.5</longitude>
<latitude>38</latitude>
<altitude>0</altitude>
<range>740933.8825924395</range>
<tilt>0</tilt>
<heading>0</heading>
</LookAt>
<Folder>
<name>Grids</name>
<Placemark>
<name>QTR</name>
<visibility>0</visibility>
<open>1</open>
<styleUrl>#linestyle1</styleUrl>
<LineString>
<coordinates>
-124.75,40,0
-124.5,40,0
-124.25,40,0
-124,40,0
-123.75,40,0
-123.5,40,0
-123.25,40,0
-123,40,0
</coordinates>
</LineString>
</Placemark>
</Folder>
</Document>
</kml>

Variable Substitution in KML Icon Reference

How can I do variable substitution in a kml icon reference? I'm using Google Earth to load the kml, and my image doesn't appear for this simple example:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Document>
<name>TestMap</name>
<Style id="Icon1">
<IconStyle>
<Icon>
<href>$[url]</href>
</Icon>
</IconStyle>
</Style>
<Placemark>
<name>Hello World</name>
<styleUrl>#Icon1</styleUrl>
<ExtendedData>
<Data name="url">
<value>http://magiccards.info/scans/en/al/232.jpg</value>
</Data>
</ExtendedData>
<Point>
<coordinates>
0,0,0
</coordinates>
</Point>
</Placemark>
</Document>
</Document>
</kml>
Variable substitution for extended data in KML only works in context of the description so you could show the placemark's data url via the description balloon.
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Data+BalloonStyle</name>
<Style id="balloon-style">
<BalloonStyle>
<text>
<![CDATA[
$[name]<br>
<img src="$[url]"/>
]]>
</text>
</BalloonStyle>
</Style>
<Placemark>
<name>Hello World</name>
<styleUrl>#balloon-style</styleUrl>
<ExtendedData>
<Data name="url">
<value>http://magiccards.info/scans/en/al/232.jpg</value>
</Data>
</ExtendedData>
<Point>
<coordinates>-111.956,33.5043</coordinates>
</Point>
</Placemark>
</Document>
</kml>
See related tutorial for adding custom data which describes using the BalloonStyle Element as a Template
https://developers.google.com/kml/documentation/extendeddata
If you want to display a custom icon via IconStyle per placemark then you need to define an inline Style for each placemark with the appropriate URL.
<Placemark>
<name>Hello World</name>
<Style>
<IconStyle>
<Icon>
<href>http://magiccards.info/scans/en/al/232.jpg</href>
</Icon>
</IconStyle>
</Style>
<Point>
<coordinates>-111.956,33.5043</coordinates>
</Point>
</Placemark>

Changing map styles in google earth

I'm adding a chloropleth US county map in Google Earth. I've made the map in qgis and exported as a KML, but the county borders become extremely thick when I open it in Google Earth. When I try to change it, it makes all of the borders and the county values uniform, and takes away the chloropleth. How can I change individual styles such as borders in Google Earth?
Here is a representative KML:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>geo_county_ma</name>
<Style id="style3">
<LineStyle>
<color>40000000</color>
<width>3</width>
</LineStyle>
<PolyStyle>
<color>ffe7d1af</color>
</PolyStyle>
</Style>
<Style id="style2">
<LineStyle>
<color>40000000</color>
<width>3</width>
</LineStyle>
<PolyStyle>
<color>fff4e6d7</color>
</PolyStyle>
</Style>
<Placemark>
<styleUrl>#style3</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-122.4361217223626,37.80089762963801 -122.431353047545,37.80152776851571 -122.4317440301943,37.80336276723568 -122.436466590289,37.80271696430888 -122.4361217223626,37.80089762963801
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
<Placemark>
<styleUrl>#style2</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-122.436124895585,37.80088925120538 -122.4313590833908,37.80150407165125 -122.4310083235047,37.79963224033016 -122.435667004224,37.79900507682146 -122.436124895585,37.80088925120538
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
If the lines/borders of polygons are too thick try changing the width value for LineStyle to 1 (or 2) whichever works best visually:
<LineStyle>
<color>40000000</color>
<width>1</width>
</LineStyle>
You can right-mouse click on each Placemark and manually change the style line width in Google Earth but suggest you change the KML in a text editor and globally replace <width>3</width> with <width>1</width>.

Using <BaloonStyle> and <LableStyle> together

I am new to KML and seem to be going round in circles. Hope someone can help.
I want to show a HTML balloon and get rid of the ugly driving directions. I am using 'BalloonStyle' to do this.
Also want to be able to hide the placemark label, so am using 'LabelStyle' to do this.
I can get these to work separately, but don't seem to be able to get them to work together to achieve the desired result.
Below is sample code which replicates the issue. Am I doing something wrong? Or do these two items just not work together? If so, is there another way to get the desired result (a HTML Balloon and a hidden label)?
Thank you
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="randomLabelColor">
<LabelStyle>
<color>ff0000cc</color>
<colorMode>random</colorMode>
<scale>1.5</scale>
</LabelStyle>
</Style>
<Style id="FEXBalloonStyle">
<BalloonStyle>
<bgColor>ffffff</bgColor>
<text><![CDATA[<b><font color="#CC0000" size="+2">$[name]</font></b>
<br><br/><font face="Courier">$[description]</font><br/><br/><br/><br/>]]></text>
</BalloonStyle>
</Style>
<Placemark>
<name>LabelStyle.kml</name>
<styleUrl>#randomLabelColor</styleUrl>
<styleUrl>#FEXBalloonStyle</styleUrl>
<Point>
<coordinates>-122.367375,37.829192,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
You can only have one styleUrl for a Placemark. If you want to have both the <LabelStyle> and <BalloonStyle> applied a single place mark, you have to put them in the same style:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Style id="randomLabelColor">
</Style>
<Style id="FEXBalloonStyle">
<LabelStyle>
<color>ff0000cc</color>
<colorMode>random</colorMode>
<scale>1.5</scale>
</LabelStyle>
<BalloonStyle>
<bgColor>ffffff</bgColor>
<text><![CDATA[<b><font color="#CC0000" size="+2">$[name]</font></b>
<br><br/><font face="Courier">$[description]</font><br/><br/><br/><br/>]]></text>
</BalloonStyle>
</Style>
<Placemark>
<name>LabelStyle.kml</name>
<styleUrl>#FEXBalloonStyle</styleUrl>
<Point>
<coordinates>-122.367375,37.829192,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>

KML + Google Earth: Make a Polygon or GroundOverlay clickable?

Pretty simple question.
I've got some Polygons and GroundOverlays defined in KML. Is there a way to specify that they should be clickable, and (in Google Earth, at least) pop up an info balloon or similar when they are clicked?
Similarly, is it possible to give polygons/GroundOverlays any sort of mouseover behavior? e.g. change the icon or color when moused over?
Yes. Giving the Placemark a name and description will make it a clickable object in Google Earth and will open with an info window showing both. You can create rollover/mouseover behavior using stylemaps, here is an example that does both:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Highlighted Icon</name>
<description>Place your mouse over the icon to see it display the new
icon</description>
<StyleMap id="exampleStyleMap">
<Pair>
<key>normal</key>
<!-- you could also use a <styleUrl> here instead of inlining -->
<Style>
<PolyStyle>
<color>7dff0000</color>
</PolyStyle>
</Style>
</Pair>
<Pair>
<key>highlight</key>
<!-- you could also use a <styleUrl> here instead of inlining -->
<Style>
<PolyStyle>
<color>7dffffff</color>
</PolyStyle>
</Style>
</Pair>
</StyleMap>
<!-- and now, a Placemark that uses the StyleMap -->
<Placemark>
<name>Roll over this polygon</name>
<description>this will show up when clicked</description>
<visibility>1</visibility>
<styleUrl>#exampleStyleMap</styleUrl>
<Polygon>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-112.3372510731295,36.14888505105317,1784
-112.3356128688403,36.14781540589019,1784
-112.3368169371048,36.14658677734382,1784
-112.3384408457543,36.14762778914076,1784
-112.3372510731295,36.14888505105317,1784
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>

Resources