Writing a KML using C# - ScreenOverlay

*爱你&永不变心* 提交于 2019-12-12 02:25:52

问题


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!).


回答1:


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.



来源:https://stackoverflow.com/questions/15376320/writing-a-kml-using-c-sharp-screenoverlay

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!