delaying balloon component in a KML code - kml

I have just started to learn how to manipulate the KML files. here is a problem that I do not know how to overcome.... I created a polygon and added a description to be shown as a balloon, but it appears as soon as Google Earth starts working before the polygon can be seen. what I want is to show the balloon from a specific distance where the polygon appears(for example in the range distance of LookAt element).
anybody knows how to manage that?
my code follows:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" mlns: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">
<LookAt>
<longitude>17.99514610290434</longitude>
<latitude>59.36293893395309</latitude>
<altitude>0</altitude>
<range>597.51212259908</range>
<tilt>52.34415598649959</tilt>
<heading>105.3974737169693</heading>
</LookAt>
<Placemark>
<name>Stadium</name>
<description>
<![CDATA[
this is <b><i>RĂ¥sunda soccer stadium</i></b>
]]>
</description>
<gx:balloonVisibility>1</gx:balloonVisibility>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<Polygon>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
17.99514610290434,59.36293893395309,100 17.99651951950199,59.36209399425741,100 17.99752330705672,59.36252751885282,100 17.99613146514916,59.36335387902954,100 17.99514610290434,59.36293893395309,100
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>

Normally you can skip rendering the polygon until you get "close" enough to it as defined by a Region element which is determined by calculating when a given area maps to a min or max # of pixels.
Also, the <gx:balloonVisibility> tag forces the description balloon to appear when the KML is loaded regardless of whether Region is active. Adding a Region direct in the KML still shows the popup balloon.
To do what you want to do, you must wrap the KML file with a second KML file with a NetworkLink with a Region that loads the seconds KML only when the region is active (aka close enough) at which time the description is displayed along with the polygon.
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<LookAt>
<longitude>17.99514610290434</longitude>
<latitude>59.36293893395309</latitude>
<altitude>0</altitude>
<heading>105.3974737169693</heading>
<tilt>52.34415598649959</tilt>
<range>597.51212259908</range>
</LookAt>
<NetworkLink>
<Region>
<LatLonAltBox>
<north>59.363792</north>
<south>59.361556</south>
<east>17.998029</east>
<west>17.994443</west>
</LatLonAltBox>
<Lod>
<minLodPixels>128</minLodPixels>
<maxLodPixels>-1</maxLodPixels>
</Lod>
</Region>
<Link>
<href>target.kml</href>
</Link>
</NetworkLink>
</Document>
</kml>
And target.kml file contains the original KML you gave:
<?xml version="1.0" encoding="ISO-8859-1"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Stadium</name>
...
</Placemark>
</kml>
You can change the distance the feature + balloon appears by adjusting the minLodPixels value and/or the size of the region. At present the feature will display when the region defined by area surrounding the polygon is at least 128 pixels on the screen. Change to 32 or 64 and/or make the region area larger to make it appear quicker.
Note Google Earth client doesn't give you a tool to edit or even see the Region bounding boxes on the map so debugging this is tricky. You can paste your KML into this tool to generate KML making the Region bounding area visible. This helps to debug Regions more easily.

Related

KML polygon, altitude mode destroys color

I am attempting to generate a KML overlay. I have a grid I am mapping over an area, with colored squares. When I have the altitude mode as 'clampToGround' everything about the polygons I am generating, including their color, works as expected. However, it's not desirable to have the grid deform to map to the ground. When I change the altitude mode such that the overlay hovers a few meters off the ground, the shape of the grid sections is correct, but the color information is discarded and the grid sections are colored black.
Here is a testable excerpt of my KML code. Here is shows the colors correct but the grid deformed to the ground. Changing the value of altitudeMode to:
absolute produces the other behavior.
Is there a way to have this at the correct altitude AND with the correct color at the same time?
<?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">
<Document id="1">
<Document id="2">
<Style id="10">
<PolyStyle id="10">
<color>0xFF0000FF</color>
<colorMode>normal</colorMode>
<fill>1</fill>
<outline>1</outline>
</PolyStyle>
</Style>
<name>Antenna Data</name>
<Camera>
<longitude>-122.30201334502573</longitude>
<latitude>37.87244901754293</latitude>
<altitude>200</altitude>
</Camera>
<Placemark id="5">
<name>region</name>
<styleUrl>#10</styleUrl>
<Polygon id="4">
<altitudeMode>clampToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing id="8">
<coordinates>-122.30243999999999,37.871973499999996,10 -122.30243999999999,37.8719265,10 -122.30238,37.8719265,10 -122.30238,37.871973499999996,10</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</Document>
</kml>
Your KML has several issues.
The polygon is missing the closing vertex. In order to be a valid KML polygon, the first coordinate set (lon,lat,alt) must be repeated as the last coordinate set, so that the polygon closes itself. For your example of a square, you would need 5 coordinate sets, with the first and the last being the same.
Try fixing that first, and if you still see coloring issues, try making sure that the vertex winding direction is correct (anti-clockwise). If it's backwards, then the darker "bottom" of the polygon will be facing upwards, and might appear dark/black. Yours appears correct from looking at the coordinates, but I could be mistaken.
Also, your color is not a valid KML color, as it appears to have an extra "0x" in front. A valid KML color needs four two-character values (each between 00 and ff), representing Alpha, Blue, Green, Red (AABBGGRR, and yes, it's backwards from HTML colors). A solid red would be: FF0000FF.
Below is an updated copy of your KML sample with altitudeMode = absolute, the closing vertex added, and the color corrected... this works for me in Earth Pro.
<?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">
<Document id="1">
<Document id="2">
<Style id="10">
<PolyStyle id="10">
<color>FF0000FF</color>
<colorMode>normal</colorMode>
<fill>1</fill>
<outline>1</outline>
</PolyStyle>
</Style>
<name>Antenna Data</name>
<Camera>
<longitude>-122.30201334502573</longitude>
<latitude>37.87244901754293</latitude>
<altitude>200</altitude>
</Camera>
<Placemark id="5">
<name>region</name>
<styleUrl>#10</styleUrl>
<Polygon id="4">
<altitudeMode>absolute</altitudeMode>
<outerBoundaryIs>
<LinearRing id="8">
<coordinates>-122.30243999999999,37.871973499999996,10 -122.30243999999999,37.8719265,10 -122.30238,37.8719265,10 -122.30238,37.871973499999996,10 -122.30243999999999,37.871973499999996,10</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</Document>
</kml>
The solution I came across was that I could simple de-activate terrain in Google Earth. This seems to work now.

Placemarks lookat range ignored

I'm using this KML file (lets call it load.kml)
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<NetworkLink>
<refreshVisibility>1</refreshVisibility>
<flyToView>1</flyToView>
<Link> <href>C:\\Users\\bridenstinek\\workspace\\AlertServlet\\resources\\outboundKML\\test.kml</href>
<refreshMode>onInterval</refreshMode>
<refreshInterval>20</refreshInterval>
</Link>
</NetworkLink>
</Document>
</kml>
To point Google Earth to a kml file (lets call it test.kml). When I load test.kml regularly (without using load.kml) the range I set works correctly (the zoom level is how I set it).
But when test.kml gets loaded using load.kml the range is incorrect. The placemark zooms in really far and ignores the range I set in the test.kml file.
Is there something overriding the range inside load.kml?
Example of test.kml:
<?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" xmlns:location="http://example.com">
<Document>
<name>Example</name>
<Style id="alertKMLStyle">
<BalloonStyle>
<text><![CDATA[<center>
<b><font color="#CC0000" size="+8">$[name]</font></b>
<br/></center><br/>
<font size="+2">$[description]</font>
<br/><br/>
]]></text>
</BalloonStyle>
</Style>
<Placemark>
<name>Example Name</name>
<description>Example Description</description>
<LookAt>
<location:coordinates>800</location:coordinates>
<longitude>2.294</longitude>
<latitude>48.858</latitude>
<altitude>0.5</altitude>
<heading>12.23742976490019</heading>
<tilt>0</tilt>
<range>115718.4889366544</range>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#alertKMLStyle</styleUrl>
<gx:balloonVisibility>1</gx:balloonVisibility>
<Point>
<gx:drawOrder>1</gx:drawOrder>
<coordinates>2.294,48.858,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
There are a few undocumented differences when you load KML directly as opposed to via a NetworkLink.
When you load the KML via the NetworLink it looks for a LookAt or Camera in the outermost element which is the Document in test.kml file.
You can either 1) copy/move the <LookAt> from the Placemark to the parent Document element in test.kml or 2) copy the <LookAt> and insert into the NetworkLink in load.kml and make flyToView=0.
If the target KML has timestamps then the behavior via Networklinks is different then loading it directly. See related issue.

how to keep polygon and name of the polygon over the polygon surface in kml

I have asked to keep polygon, name of the polygon and the corresponding icon in the places panel.
Can any suggest me how to do this in kml?
By default you only see the name label if your placemark has a Point geometry so for lines and polygons the name is not shown.
You can however create a MultiGeometry with both a Polygon and a Point (typically the center point or where you want to label to appear near). If you don't want the default yellow pushpin to appear just add a Style with an empty IconStyle href element to the Placemark.
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>polygon</name>
<Style>
<IconStyle>
<Icon/>
</IconStyle>
</Style>
<MultiGeometry>
<Point>
<coordinates>-122.4317195,37.801848</coordinates>
</Point>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-122.43193945401,37.801983684521
-122.431564131101,37.8020327731402
-122.431499536494,37.801715236748
-122.43187136387,37.8016634915437
-122.43193945401,37.801983684521
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</kml>
This multi-geometry method can be HIGHLY tedious if you have a couple hundred polygons (say, a grid/index) and you want to show their names.
My preference is to use something like QGIS/Arc, where I create a new vector file of polygon centroids (point file) that has the polygon attributes (including name/description). I save the new point file as a point KML and import into Google Earth. From their, I set a shared style for the point file, with the icon set to 0% opacity.
Example of Google Earth Polygons with Labels using above method

Animating a line in Google Earth using kml

I am pretty accomplished at using the animated update function of google earth and am using it to move models around. What I would really like to do is to be able to animate a line (eg up and down) in Google Earth but am finding this tricky.
I have the longitude and latitude of the line at the start. For example line coordinates are:
-88,17,100 -88.20270841086835,17.21899813162266,100
I then want to raise one end of this line up to an altitude of 500 over a period of 5 seconds.
I've drawn the line using LineString:
<Placemark id="path1">
<name>Untitled Path man</name>
<LineString>
<tessellate>1</tessellate>
<coordinates>
-88.,17,100 -88.20270841086835,17.21899813162266,100
</coordinates>
</LineString>
</Placemark>
But Im now lost as to how to use <gx:AnimatedUpdate> to move one end up from 100 to 500.
Im sure its easy - can someone point me in the right direction??
The trick is to update the LineString element (with an id on that) rather than the Placemark.
Here's a working KML example tour that animates a line changing from a relative altitude of 100 to 500m.
<?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">
<Document>
<name>gx:AnimatedUpdate example</name>
<open>1</open>
<LookAt>
<longitude>-88.1351880996469</longitude>
<latitude>17.09943637744042</latitude>
<altitude>0</altitude>
<heading>49.91874373078863</heading>
<tilt>84.43764019949967</tilt>
<range>1929.311316966288</range>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<Placemark>
<name>Untitled Path man</name>
<LineString id="path1">
<tessellate>1</tessellate>
<altitudeMode>relativeToGround</altitudeMode>
<coordinates>
-88,17,100 -88.20270841086835,17.21899813162266,100
</coordinates>
</LineString>
</Placemark>
<gx:Tour>
<name>Play me!</name>
<gx:Playlist>
<gx:AnimatedUpdate>
<gx:duration>5</gx:duration>
<Update>
<targetHref/> <!-- Left empty to refer to the current file -->
<Change>
<LineString targetId="path1">
<coordinates>
-88,17,100 -88.20270841086835,17.21899813162266,500
</coordinates>
</LineString>
</Change>
</Update>
</gx:AnimatedUpdate>
<!-- Wait for the animation to complete (see the touring
tutorial for an explanation of how AnimatedUpdate's
duration isn't enough to guarantee this). -->
<gx:Wait>
<gx:duration>5.0</gx:duration>
</gx:Wait>
</gx:Playlist>
</gx:Tour>
</Document>
</kml>
For details see https://developers.google.com/kml/documentation/touring#tourtimelines

Display a hierarchy of placemarks in google earth

I am trying to use kml to implement a heirarchy of regions as describe in
KML 2.1 Tutorial
I would like to have a feature(polygon or icon) representing a region and when the region becomes active I would like to display a feature representing each of the regions immediate children but no longer display the icon representing the parent region.
An example would be if I had a region representing Canada and a placemark in the middle of the region. When the Canada region becomes active I want the canada placemark to disappear and want to display placemarks over the provinces of Canada(a region and placemark for BC, Alberta ect.). Then when the BC region became active I would like to replace the bc placemark with placemarks for cities in bc.
Any help would be greatly appreciated.
Thank you.
You are on the right track, Regions are what you want. Check out this page for more info on how to use them
http://code.google.com/apis/kml/documentation/regions.html
In particular, you need to understand the Level of Detail (LOD). The LOD determines the view boundaries that activates and deactivates the region. In the end you need to create Regions in this format.
<Region>
<LatLonAltBox>
<north>50</north>
<south>45</south>
<east>28</east>
<west>22</west>
</LatLonAltBox>
<Lod>
<minLodPixels>128</minLodPixels>
<maxLodPixels>1024</maxLodPixels>
</Lod>
</Region>
You can set -1 so the region is ALWAYS shown, no matter how far someone zooms OUT or if you use the value above (128) that means that the bounding box you set, must take up 128x128 pixels of the viewers screen before it gets activated (seen).
The is what you use to turn the view off as they zoom in. Or leave at -1 so it never turns off no matter how far someone zooms IN.
For your question, you would have a region that contains a placemark in the middle of Canada. That region would have a minlodpixels of -1 and a maxlodpixels that corresponds with the minlodpixels of another region (which shows the placemarks in the provinces) I would make a region for each province seperately.
The trick is working out the boundaries of the - I use a square polygon with four points and then look at its code to extract the etc
Actually, here is a great page to show you how to create regions - make sure you download the kml called 'Screen Overlay Size Guide' - it makes things a LOT easier
http://earth.google.ca/intl/en_ca/outreach/tutorial_region.html
I am trying to do the same thing and came up with a solution that works however it requires many calls to the server.
In your base kml loaded from http://example.com/zones.kml you would have
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document id="base">
<Folder id="1_folder">
//Region responsible for hiding the current (Canada) Placemark when zoomed in and made inactive ALSO responsible for hiding children(BC, Alberta...) Placemarks when active
<Region>
<LatLonAltBox>
<north>74.79903411865234</north>
<south>41.508766174316406</south>
<east>-52.03630065917969</east>
<west>-139.96746826171875</west>
</LatLonAltBox>
<Lod>
<maxLodPixels>
1024
</maxLodPixels>
</Lod>
</Region>
//NetworkLink that hides children Placemarks when zooming out
<NetworkLink>
<refreshVisibility>1</refreshVisibility>
<Link>
<href>http://example.com/1/hide_children.kml</href>
<viewRefreshMode>onRegion</viewRefreshMode>
</Link>
</NetworkLink>
//The Canada Placemark
<Placemark id="1">
<name>Canada</name>
<Point>
<coordinates>-96.00188446044922,58.153900146484375</coordinates>
</Point>
</Placemark>
//Networklink for Loading Children While Zooming in
<NetworkLink>
<refreshVisibility>1</refreshVisibility>
// Same Region as above but with minLodPixels instead of maxLodPixels
<Region>
<LatLonAltBox>
<north>74.79903411865234</north>
<south>41.508766174316406</south>
<east>-52.03630065917969</east>
<west>-139.96746826171875</west>
</LatLonAltBox>
<Lod>
<minLodPixels>1024</minLodPixels>
</Lod>
</Region>
<Link>
<href>http://example.com/zones/1.kml</href>
<viewRefreshMode>onRegion</viewRefreshMode>
</Link>
</NetworkLink>
</Folder>
</Document>
</kml>
This requires two more kml files that are loaded to hide or show the children of a particular placemark.
The kml for hiding the children would be loaded from http://example.com/1/hide_children.kml and contains the networklinkcontrol to hide the children
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<NetworkLinkControl>
<Update>
<targetHref>http://example.com/zones.kml</targetHref>
<Change>
<Folder targetId="1_children"><visibility>0</visibility></Folder>
</Change>
</Update>
</NetworkLinkControl>
</kml>
The kml for showing the children would be loaded from http://example.com/1.kml and contains the networklinkcontrol to show the children the children
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<NetworkLinkControl>
<Update>
<targetHref>http://example.com/zones.kml</targetHref>
<Create>
<Folder targetId="1_folder">
<Folder id='1_children'>
//This folder is the same as the original zone
<Folder id="2_folder">
//Empty region if the zone has no children
<Region></Region>
//No need for networklink to hide children if zone has no children
//Placemark
<Placemark id="2">
<name>BC</name>
<Point>
<coordinates>-122.95623779296875, 50.06098937988281</coordinates>
</Point></Placemark>
</Folder>
//Same for other Provinces
//<Folder id="3_folder"></Folder>
//<Folder id="4_folder"></Folder>
</Folder>
</Folder>
</Create>
</Update>
</NetworkLinkControl>
</kml>
It would be best if you could define 2 different regions one for minLod and one for maxLod so you didn't need to have so many calls to servers and you didn't have to call the server to load children you have already loaded but I have not figured out how to do this yet.
This need to be optimized quite a bit. Please let me know if anyone can help find a more direct way of doing this. Thanks

Resources