如何使用SharpKML在kml中插入新的地标

时间:2014-06-23 09:39:34

标签: google-maps kml sharpkml

我遇到KMLSharp问题。我想使用SharpKML为我的kml文件添加一个新的地标。

这是我正在使用的代码。

    // Create our Kml
    TextReader reader = File.OpenText(Server.MapPath(@"~\kml\fountains\test1.kml"));

    KmlFile file = KmlFile.Load(reader);

    reader.Close();

    Kml kml = file.Root as Kml;

    //This is the Element we are going to save to the Kml file.
    Point point = new Point();
    point.Coordinate = new Vector(double.Parse(latitude), double.Parse(longitude));

    var placemark = new Placemark();
    placemark.Geometry = point;
    placemark.Id = "7";        

    var document = new Document();
    document.AddFeature(placemark);

    var update = new Update();
    update.AddUpdate(new CreateCollection() { document });

    CreateCollection c = new CreateCollection();

    var serializer = new Serializer();
    serializer.Serialize(update);
    Console.WriteLine("\nUpdate:\n" + serializer.Xml);

    // Run the update        
    file = KmlFile.Create(kml, false);
    update.Process(file);

    serializer.Serialize(kml);
    Console.WriteLine("\nUpdated Kml:\n" + serializer.Xml);

    using (FileStream stream =        
 **strong text**   File.OpenWrite(Server.MapPath(@"~\kml\fountains\test1.kml")))
    {
        file.Save(stream);
    }

这是我的KML文件结构:

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns:gx="http://www.google.com/kml/ext/2.2"   xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom"  xmlns="http://www.opengis.net/kml/2.2">
  <kml:Document>
    <kml:Style id="normalPlacemark">
      <kml:IconStyle>
        <kml:Icon>
          <kml:href>http://url.com/kml/WaterFountain.png</kml:href>
        </kml:Icon>
    <kml:hotSpot x="0.5" xunits="fraction" y="0" yunits="fraction" />
  </kml:IconStyle>
</kml:Style>
<kml:Placemark id="1">
  <kml:styleUrl>#normalPlacemark</kml:styleUrl>
  <kml:Point>
    <kml:coordinates>25.6700345134745,-34.0089981824299</kml:coordinates>
  </kml:Point>
</kml:Placemark>
<kml:Placemark id="2">
  <kml:styleUrl>#normalPlacemark</kml:styleUrl>
  <kml:Point>
    <kml:coordinates>25.6703427255479,-34.0086888747824</kml:coordinates>
  </kml:Point>
</kml:Placemark>
<kml:Placemark id="3">
  <kml:styleUrl>#normalPlacemark</kml:styleUrl>
  <kml:Point>
    <kml:coordinates>25.6696969558545,-34.0083855123115</kml:coordinates>
  </kml:Point>
</kml:Placemark>
<kml:Placemark id="4">
  <kml:styleUrl>#normalPlacemark</kml:styleUrl>
  <kml:Point>
    <kml:coordinates>25.6697452356168,-34.0090202999215</kml:coordinates>
  </kml:Point>
</kml:Placemark>
</kml:Document>
</kml>

我认为这可能与我放置地标的容器有关,因为我无法在没有容器的情况下添加地标:/

0 个答案:

没有答案