2

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 :/

geocodezip
  • 158,664
  • 13
  • 220
  • 245
user3410255
  • 21
  • 1
  • 3
  • Why are you adding the kml: namespace to everything? That seems to be what is confusing the KML parsers. – geocodezip Jun 23 '14 at 18:29
  • This [http://url.com/kml/WaterFountain.png](http://url.com/kml/WaterFountain.png) is not a valid icon URL, you won't see any icons. – geocodezip Jun 23 '14 at 18:56
  • My original kml file didn't have that in, it's only once it went through the sharpKML parser when that got added! As to the invalid url, all my icons are showing? What would the correct url be? – user3410255 Jun 24 '14 at 08:55
  • ooooh and thanks for the responses thus far :) – user3410255 Jun 24 '14 at 08:56

0 Answers0