How do you insert a new placemark in a kml with SharpKML

3.2k views Asked by At

I am having a problem with KMLSharp. I want to add a new placemark to my kml file using SharpKML.

This is the code I am using.

    // 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);
    }

This is my KML File Structure:

<?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>

I think it might have something to do with the container I am placing the placemarker in since I can't just add the placemarker without a container :/

0

There are 0 answers