首先新建一个项目,添加一个timer控件和按钮,当点击按钮时,会动态更新dynamic.kml中的内容,使点的经纬度坐标实时变化。代码如下:
private double xx=100; private double yy=30; private void timer1_Tick(object sender, EventArgs e) { xx +=0.03; yy +=0.03; FileStream fs = new FileStream("products.kml", FileMode.Create); XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8); // Start the document. w.WriteStartDocument(); w.WriteStartElement("kml", "http://earth.google.com/kml/2.1"); w.WriteStartElement("Document"); w.WriteStartElement("name"); w.WriteString("zy's KML"); w.WriteEndElement(); w.WriteStartElement("open"); w.WriteString("1"); w.WriteEndElement(); //style w.WriteStartElement("Style"); w.WriteAttributeString("id", "normalPlacemark"); w.WriteStartElement("IconStyle"); w.WriteStartElement("Icon"); w.WriteStartElement("href"); w.WriteString("F:/img/web.gif"); w.WriteEndElement();//style w.WriteEndElement();//iconStyle w.WriteEndElement();//icon w.WriteEndElement();//href //stylemap w.WriteStartElement("StyleMap"); w.WriteAttributeString("id", "exampleStyleMap"); w.WriteStartElement("Pair"); w.WriteStartElement("key"); w.WriteString("normal"); w.WriteEndElement(); w.WriteStartElement("styleUrl"); w.WriteString("#normalPlacemark"); w.WriteEndElement();//styleUrl w.WriteEndElement();//pair w.WriteEndElement();//StyleMap w.WriteStartElement("Placemark"); w.WriteStartElement("Name"); w.WriteString("zy's KML"); w.WriteEndElement(); // Write Point element w.WriteStartElement("Point"); w.WriteStartElement("coordinates"); w.WriteString(xx.ToString ()+","+yy.ToString ()); w.WriteEndElement(); w.WriteEndElement(); w.WriteEndElement(); // Placemark w.WriteEndElement();//document w.WriteEndElement(); // kml // Ends the document. w.WriteEndDocument(); // close writer w.Close(); }
然后新建RealtimeNetlink.kml文件,每次kml刷新的时候,都重新调用dynamic.kml内容,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<name>RealtimeNetlink </name>
<visibility>1</visibility>
<open>1</open>
<NetworkLink>
<visibility>1</visibility>
<refreshVisibility>1</refreshVisibility>
<flyToView>0</flyToView>
<LookAt>
<longitude>114.12022</longitude>
<latitude>22.55841667</latitude>
<altitude>2000</altitude>
<range>1000</range>
<tilt>0</tilt>
<heading>0</heading>
<altitudeMode>relativeToGround</altitudeMode>
</LookAt>
<Link>
<href>\dynamic.kml</href>
<refreshMode>onInterval</refreshMode>
<refreshInterval>3</refreshInterval>
<viewRefreshMode>onStart</viewRefreshMode>
<viewRefreshTime>3</viewRefreshTime>
</Link>
</NetworkLink>
</Document>
</kml>