Search code examples
google-mapskmlgoogle-earth

How do I refer in a kml script within a ballon-text or a description-text to the text from another ballon or description part?


I have a KML-script with some placemarks which have some content written in the "description" part (in HTML-style). Everything works fine, but in some description-parts I want to include a link/reference that points to the description-part of another placemark within the KML-script.

When the user clicks on a placemark that points to Paris, a ballon with the description content opens (this already works fine). What I want to achieve is that within this ballon the user should be able to click on a highlighted word, e.g. "let's move to Marseille", and after doing so the Paris-Ballon should close while the Marseille-Ballon - which belongs to the description-part of the Marseille-placemark - should open.

Is that possible? I couldn't find anything about that in the documentation or with googling. (Or I'm just too stupid to find it).

Cheers, Emma


Solution

  • You can link from one Placemark to another using special links in the description where one references the other. The mechanism is called a feature anchor in the KML standard.

    Note the URL in Paris placemark is #marseille;balloonFlyto where the "id" attribute of the target Placemark is "marseille" and the target action to take when clicked is "balloonFlyto".

    Here is complete KML with one placemark linking to another:

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2">
      <Document>
        <name>feature anchor</name>
        <description>Feature anchor using ; to delimit action.</description>
    
        <Placemark id="paris">
          <name>Paris</name>
            <description>
            <![CDATA[ 
            let's move to <a href="#marseille;balloonFlyto">Marseille</a>
          ]]>
          </description>
          <Point>
            <coordinates>2.3508,48.8567</coordinates>
          </Point>
        </Placemark>
    
        <Placemark id="marseille">
          <name>Marseille</name>
          <description>
            <![CDATA[ 
            Welcome to Marseille.
            Return to <a href="#paris;balloonFlyto">Paris</a>       
          ]]>
          </description>
          <Point>
            <coordinates>5.37,43.2964</coordinates>
          </Point>
        </Placemark>
    
          </Document>
    </kml>
    

    The target href in the popup description balloon can be a fragment URL (that is, a URL with a # sign followed by a KML identifier). You can also append an action to the URL with a semi-colon (;) and one of these qualifiers:

    • flyto (default) - fly to the Feature
    • balloon - open the Feature's balloon but do not fly to the Feature
    • balloonFlyto - open the Feature's balloon and fly to the Feature

    Excerpt above from KML reference.