Monday, February 18, 2013

Example KML for displaying extended data in balloons

In Google Earth, it is possible to display a nicely formatted Placemark balloon showing some relevant information about that Placemark. The simplest way to create the Placemark KML is to write and format everything (including the HTML mark ups) in the Placemark's Description tag, as shown in the example code below.
<Placemark>
<name>Microsoft</name>
<description>
<![CDATA[
<b>Example 1</b>
<table border="1" >
<tr><td>Company Name</td></tr>
<tr><td><i>Microsoft</i></td></tr>
</table>
]]>
</description>
<Point> <coordinates>-122.034924,37.331586,0</coordinates>
</Point>
</Placemark>

The screenshot shows how it is displayed in Google Earth.

However, if there are a lot of Placemarks, it would be a pain to have write the mark up and data values for everyone. It would be better to define a custom balloon style containing a template HTML mark up and placeholders for the variable data values once; the data values would then be stored under the Placemark's ExtendedData tag. When Google Earth renders the Placemark balloons, it would use the referenced balloon style and replace the placeholders with the actual data values from the Placemark's ExtendedData tags.

The example code below defines a balloon style with a placeholder $[Company_Name] in the HTML mark up text.
<Style id="MyBalloonStyle">
<BalloonStyle>
<text> <![CDATA[
<b>Example extended data template</b>
<table border="1" >
<tr><td>Company Name</td></tr>
<tr><td><i>$[Company_Name]</i></td></tr>
</table>
]]></text>
<bgColor>ffffffbb</bgColor>
</BalloonStyle>

Then in the Placemark tag, it is only necessary to place the data (name and value) under the ExtendedData tag as shown below. The balloon style is referenced with the StyleUrl tag.
<Placemark>
<name>Apple</name>
<styleUrl>#MyBalloonStyle</styleUrl>
<ExtendedData>
<Data name="Company_Name"><value>Apple Incorporated</value></Data>
</ExtendedData>
<Point> <coordinates>-122.034924,37.331586,0</coordinates>
</Point>
</Placemark>

The full KML code is shown below.


<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
 
<Style id="MyBalloonStyle">
<BalloonStyle>
<text> <![CDATA[
<b>Example extended data template</b>
<table border="1" >
<tr><td>Company Name</td></tr>
<tr><td><i>$[Company_Name]</i></td></tr>
</table>
]]></text>
<bgColor>ffffffbb</bgColor>
</BalloonStyle>
<IconStyle> <color>ffffffff</color> <scale>1</scale>
<Icon><href>http://maps.google.com/mapfiles/kml/pushpin/grn-pushpin.png</href>
</Icon>
</IconStyle>
<LabelStyle> <scale>0</scale> </LabelStyle>
</Style>
<Folder> <name>Tech Companies</name>
<Placemark>
<name>Apple</name>
<styleUrl>#MyBalloonStyle</styleUrl>
<ExtendedData>
<Data name="Company_Name"><value>Apple Incorporated</value></Data>
</ExtendedData>
<Point> <coordinates>-122.034924,37.331586,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Google</name>
<styleUrl>#MyBalloonStyle</styleUrl>
<ExtendedData>
<Data name="Company_Name"><value>Google Incorporated</value></Data>
</ExtendedData>
<Point> <coordinates>-122.084676,37.421742,0</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>

The screenshot shows how it is rendered in Google Earth.

No comments: