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>
Related
Someone shared a kml with me that was created in Google Earth Web along with a screenshot of it.
When I opened it up in Google Earth Pro, the colored polygons displayed as solid white.
For a test, I created a test polygon in Google Earth Web and exported it as KML.
Projects > New project > Create KML file
Draw line or shape > create polygon
Set fill color red at 75%
Set color 4px
Select export as KML
Same thing, the polygon shows up as a white box in Google Earth Pro.
The exported KML is structured like this:
<?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>
<Document>
<name>Untitled Project</name>
<gx:CascadingStyle kml:id="__managed_style_147D324643211BD21CEE">
<Style>
<IconStyle>
<Icon>
<href>https://earth.google.com/earth/rpc/cc/icon?color=1976d2&id=2000&scale=4</href>
</Icon>
<hotSpot x="64" y="128" xunits="pixels" yunits="insetPixels"/>
</IconStyle>
<LabelStyle>
</LabelStyle>
<LineStyle>
<color>ffa21f7b</color>
<width>3.63636</width>
</LineStyle>
<PolyStyle>
<color>c02f2fd3</color>
</PolyStyle>
<BalloonStyle>
<displayMode>hide</displayMode>
</BalloonStyle>
</Style>
</gx:CascadingStyle>
...
<StyleMap id="__managed_style_0C1991940B211BD21CEE">
<Pair>
<key>normal</key>
<styleUrl>#__managed_style_147D324643211BD21CEE</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#__managed_style_2EB2C27FFE211BD21CEE</styleUrl>
</Pair>
</StyleMap>
<Placemark id="09A507D5E3211BD1D53F">
<name>Test Polygon</name>
<styleUrl>#__managed_style_0C1991940B211BD21CEE</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
0.0287244781654028,51.5286273373769,4.888032506075639 0.03124375777183053,51.52899071640604,5.649232085770059 0.03060716674301966,51.5305703663013,6.296306355936279 0.02835525808849049,51.53027540191405,5.649128389213159 0.0287244781654028,51.5286273373769,4.888032506075639
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
The <gx:CascadingStyle> does not appear in the KML reference.
https://developers.google.com/kml/documentation/kmlreference
What is going on here?
i am expecting the original colored polygon to appear in Google Earth Pro.
The <gx:CascadingStyle> is an undocumented element that is created in Google Earth Web that is unsupported by Google Earth Pro.
If you make a minor edit to the KML, then it will work as expected.
Make these changes:
Remove <Style> and </Style> elements.
Globally change <gx:CascadingStyle kml:id="xxx"> to <Style id="xxx">.
Replace <gx:CascadingStyle kml:id= with <Style id=
Replace </gx:CascadingStyle> with</Style>
Example:
Old:
<gx:CascadingStyle kml:id="__managed_style_147D324643211BD21CEE">
<Style>
...
</Style>
</gx:CascadingStyle>
New:
<Style id="__managed_style_147D324643211BD21CEE">
...
</Style>
You can globally make the changes to the KML using a text editor; e.g. Notepad++.
The KML is now valid with respect to the OGC KML 2.2 standard and will work in Google Earth Pro.
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
I am using the following code to generate a line in Google Earth, however the formatting of the line is not changing.
<Placemark>
<LineString>
<Style id="bendigo_line">
<LineStyle>
<width>3</width>
<color>64F0FF14</color>
</LineStyle>
</Style>
<coordinates>
-15,52,0
-20,53,0
-30,53,0
-40,53,0
-50,52,0
-53.233333,51.166667
</coordinates>
</LineString>
</Placemark>
I'm unsure of what I am doing wrong as this method has worked in the past and now doesn't change the line from the default thickness and colour.
Thanks
The <Style> element must appear outside the <LineString> element. KML requires elements in a particular order.
For the syntax of the Placemark refer to the KML Reference.
Try changing your KML to this order of elements:
<Placemark>
<Style>
<LineStyle>
<width>3</width>
<color>64F0FF14</color>
</LineStyle>
</Style>
<LineString>
<coordinates>
...
</coordinates>
</LineString>
</Placemark>
When there are problems with KML try first to validate it using the KML Validator.
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>.
I am able to display polygons, circles, etc etc, using KML.
Now i want to display only some Names using KML. Is this Possible ?
If you want to suppress displaying the label of placemarks (via KML) on the map of Google Earth then you can add a LabelStyle to your placemarks with a 0 scale (see sn_hide style in example below). If you want to suppress the label name on the map until you hover over the icon then StyleMaps are your best bet.
The first placemark in example below has its name shown in the places panel but hidden from the map using the LabelStyle. The second placemark #2 uses a StyleMap to hide the label until the user highlights or mouses over the icon in which it activates the highlight style showing the label. The third placemark #3 uses the default style that always shows the label.
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Hide and show labels</name>
<Style id="sn_hide">
<LabelStyle>
<scale>0</scale>
</LabelStyle>
</Style>
<Style id="sh_style">
<LabelStyle>
<scale>1.1</scale>
</LabelStyle>
</Style>
<StyleMap id="msn_hide">
<Pair>
<key>normal</key>
<styleUrl>#sn_hide</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_style</styleUrl>
</Pair>
</StyleMap>
<Placemark>
<name>Placemark 1</name>
<description>Label name always hidden</description>
<styleUrl>#sn_hide</styleUrl>
<Point>
<coordinates>-119.232195,36.016021</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Placemark 2</name>
<description>Hover over place to show label</description>
<styleUrl>#msn_hide</styleUrl>
<Point>
<coordinates>-119.2324,36.0155</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Placemark 3</name>
<description>Always showing</description>
<Point>
<coordinates>-119.232672,36.014837</coordinates>
</Point>
</Placemark>
</Document>
</kml>