Results 1 to 7 of 7

Thread: Overlay: How to add a legend?

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    11

    Default Overlay: How to add a legend?

    I'm just creating an overlay visualizing statistical data by colors (similar to a rain overlay using dark blue for a rain forrest, and white for a desert - but in my case for a different purpose). Now I got the overlay working almost fine (the image used seems not to be 100% scaled, but it works), so I can use it. But to give it to someone else, I'd need to add a legend explaining the colors used.

    What I want the elgend to be like (ideal case) is:
    • using an image stored inside the .kmz file holding the legend info
    • fixed position relative to the window, not to the map
    • automatically "pop up" when the overlay is displayed, and automatically close when it's no longer on the screen
    • user should be able to manually close it if not needed


    Is this somehow possible - and if, how to achieve it?

    Thank you in advance for your help and suggestions,
    Izzy.

  2. #2
    Junior Member
    Join Date
    Nov 2008
    Posts
    11

    Default

    Well - looks that I was faster, and found out myself how to do this manually by manipulating the .kmz contents with a text editor, which I will describe here shortly.

    I'm still interested if and how that can be done with the GE app directly!

    So here is how it can be done:

    Things you need:
    • a toll to pack/unpack ZIP archives (Linux: the freeware zip/unzip apps; Windows: WinZip or use the built-in Zip support)
    • a text editor capable of UTF-8
    • your graphic files (i.e. the legend as *.png file)


    Things to do:
    1. Create an empty folder and extract the contents of you .kmz file into it
    2. Copy your legend.png file into the subfolder named "files"
    3. Open the doc.kml file from that folder with the text editor and apply the changes mentioned below
    4. pack the contents of the folder (including the subdirectory) with your Zip tool and make sure the file gets the .kmz extension


    Sounds quite easy, right? And now for the changes to be applied to the .kml file: First check how it looks like between the opening and closing "<kml>" tags. If there is only one element inside (for an overlay, this is usually something enclosed by the "<GroundOverlay>" tag), you need to surrount it by an additional "<Document>...</Document>" tag - the reason is that the KML specification only allows one top-level element, and we want to add a second. Now for the element to add, which I will explain using an example:

    Code:
    <kml xmlns="http://earth.google.com/kml/2.2">
    <Document>
    <GroundOverlay>
    ...
    </GroundOverlay>
    <ScreenOverlay id="Legend">
      <name>Legend</name>
      <description>Legend for my overlay</description>
      <icon>files/legend.png</icon>
      <overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>
      <screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>
      <size x="-1" y="-1" xunits="fraction" yunits="fraction"/>
      <rotation>0</rotation>
      <visibility>1</visibility>
      <open>0</open>
    </ScreenOverlay>
    </Document>
    So what is that? Directly in front of the existing element I added the opening "<Document>" tag. Everything from "<GroundOverlay>" to "</GroundOverlay>" I left untouched (hence the "..."). And now comes the element for the legend. You all can guess what "name" and "description" is for. The "icon" tag is the image file to use for the legend, which we placed into the "files" subfolder. With the "overlayXY" and "screenXY" I placed the legend in the upper left corner of the screen. The size tag tells GE to keep the original dimensions ("-1"). We don't want the image to be rotated (makes no sense unless your text is rotated on the image file used itself), and the legend shall be visible initially. That's all.

    If you want to play more with it, or dig into the details, you can check out the KML Reference.

    Good luck!
    Izzy.

  3. #3
    Junior Member
    Join Date
    Nov 2008
    Posts
    11

    Default

    P.S.: You may want to replace the "Document" tag with the "Folder" tag - so you can easily switch the visibility of the entire package, instead of (de)activating the overlay and the legend separately.

    Best regards,
    Izzy.

  4. #4
    Junior Member
    Join Date
    Apr 2009
    Posts
    2

    Default Html into the legend

    Hi!

    An image is the only way of adding a legend as ScreenLayer? I mean, is there a way of creating it with html, for example?

    thanks in advance

  5. #5
    Junior Member
    Join Date
    Nov 2008
    Posts
    11

    Wink Use ExtendedData - and use the API reference ;)

    Looks like you missed my link to the reference - or you didn't follow it? You may try to play with ExtendedData attributes. Would be nice if you report your results here.

  6. #6
    Junior Member
    Join Date
    Apr 2009
    Posts
    2

    Default

    Well, I'm afraid that the API web does not work correctly with Firefox (at least, with my firefox). I supposed and have found out it works perfectly with Chrome.

    Ok, I've tried the ExtendedData but I think it is not exactly what I need. I'd like to have a ScreenOverlay like...

    <ScreenOverlay>
    <name>Legend name</name>
    <color>ffffffff</color>
    <Icon>
    <href>http://server.com/logo.png</href>
    </Icon>
    <overlayXY x="0" y="1" xunits="fraction" yunits="fraction"/>
    <screenXY x="0" y="1" xunits="fraction" yunits="fraction"/>
    <rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/>
    <size x="0" y="0" xunits="fraction" yunits="fraction"/>
    </ScreenOverlay>

    The image appears great, it's png and its transparency makes a nice effect. It's at the top left corner, but I'd like to have the legend below it, if possible with HTML like as in...

    [the logo as described]
    <p>My html legend...</p>
    <ul>
    <li>[color1] 0..10</li>
    <li>[color2] 11..20</li>
    <li>[color3] 21..30</li>
    ...
    </ul>

    After that, I'll have to think how add some CSS to disable list-balls and add colors so it's a proper legend...

    Thanks in advance!

  7. #7

    Default

    You could always add a legend to the info balloon with html.

    Matt
    Matt Fox

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •