adam
adam

Reputation: 13

Writing a KML using C# - ScreenOverlay

I am trying to get an overlay on google maps using a program I am making in C#. I can't seem to figure out the code to get the position of the overlay where I want it. It just sits right in the middle of the page no matter what I try.

Here is my code:

kml.WriteStartElement("ScreenOverlay");
kml.WriteElementString("name", "elephant");
kml.WriteStartElement("Icon");
kml.WriteElementString("href", "images/elephant.jpg");

//This is the part I can't figure out below

kml.WriteStartElement("overlayXY", "x='0' y='0' xunits='fraction' yunits='fraction'/");
kml.WriteStartElement("screenXY", "x='0' y='0' xunits='pixels' yunits='pixels'/");
kml.WriteStartElement("rotationXY", "x='0' y='0' xunits='fraction' yunits='fraction'/");
kml.WriteStartElement("size", "x='0' y='0' xunits='pixels' yunits='pixels'/");

kml.WriteEndElement();
kml.WriteEndElement();
kml.WriteEndElement();
kml.WriteEndElement();
kml.WriteEndElement();
kml.WriteEndElement();

That code was my latest attempt. It didn't work though. The "elephant" image still remains in the middle of the screen. I'm a beginner (if it wasn't obvious!).

Upvotes: 0

Views: 1413

Answers (1)

CodeMonkey
CodeMonkey

Reputation: 23748

If you're using the System.Xml.XmlTextWriter class in C# to generate KML ouput then the second argument of WriteStartElement() is the element namespace not the attributes and the Icon element must end before starting the overlayXY element otherwise the generated output is invalid KML.

The syntax is this:

[C#]
public void WriteStartElement(
 string localName,
 string ns
);

You need instead to change the code into this:

  XmlTextWriter kml = new XmlTextWriter(...)
  kml.Formatting = Formatting.Indented;
  kml.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
  kml.WriteStartElement("ScreenOverlay");
  kml.WriteElementString("name", "elephant");
  kml.WriteStartElement("Icon");
  kml.WriteElementString("href", "images/elephant.jpg");
  kml.WriteEndElement(); // Icon

  kml.WriteStartElement("overlayXY");    
  kml.WriteAttributeString("x", "0");
  kml.WriteAttributeString("y", "0");
  kml.WriteAttributeString("xunits", "fraction");
  kml.WriteAttributeString("yunits", "fraction");
  kml.WriteEndElement(); // overlayXY

  kml.WriteStartElement("screenXY");
  kml.WriteAttributeString("x", "0");
  kml.WriteAttributeString("y", "0");
  kml.WriteAttributeString("xunits", "pixels");
  kml.WriteAttributeString("yunits", "pixels");
  kml.WriteEndElement(); // screenXY
  ...
  kml.WriteEndElement(); // ScreenOverlay
  kml.WriteEndElement(); // kml


TIP: When you generate KML with an application (other than Google Earth) you should always validate your KML. You can post your KML to Galdos KML Validator, which will report any errors not only to the KML XML Schema but also pertaining to the OGC KML Specifiation.

Upvotes: 2

Related Questions