Multiple links

Jun 6, 2011 at 4:35 PM

Hi there

You library looks nice and tidy. I am looking for some advanced examples if you have any. 

I am specifically looking for this type of structure:

Main KML points to many links

Each link has many real time updated or added placemarks.

I do not see how to add multiple links to a kml file(or even one for that matter)?

I am still learning KML and it's rules so maybe what I am trying to do is not allowed.

 

Thanks in advance!

Coordinator
Jun 6, 2011 at 11:40 PM

I think what you're trying to achieve is something like the example given here. The Link must be contained by a NetworkLink, which in turn is contained by a Container. If you use a Document then you can group together more than one Feature (NetworkLink derives from Feature) so you could do something like this in code:

using System;
using SharpKml.Base;
using SharpKml.Dom;
using SharpKml.Engine;

class Program
{
    static void Main(string[] args)
    {
        NetworkLink networkLink = new NetworkLink();
        networkLink.Link = new Link();
        networkLink.Link.Href = new Uri("http://127.0.0.1/myfile.kml");

        Document document = new Document();
        document.AddFeature(networkLink);
        document.AddFeature(networkLink.Clone());

        Kml root = new Kml();
        root.Feature = document;

        Serializer serializer = new Serializer();
        serializer.Serialize(root);
        Console.WriteLine(serializer.Xml);
    }
}

Which outputs:

<?xml version="1.0" encoding="utf-16"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <NetworkLink>
      <Link>
        <href>http://127.0.0.1/myfile.kml</href>
      </Link>
    </NetworkLink>
    <NetworkLink>
      <Link>
        <href>http://127.0.0.1/myfile.kml</href>
      </Link>
    </NetworkLink>
  </Document>
</kml>

As for processing the updates (i.e. changing the KML), once you've built your Update object and loaded that then you can use the Process extension method in the SharpKml.Engine namespace to add/delete/replace parts of your original KML.

Hope that gives you a start in the right direction but if you have any problems then please reply below.

Thanks for trying the library and your feedback,

Sam

Jun 7, 2011 at 3:29 PM

Wonderful post. Can't wait to get this in and I am not sure how I did not realize that a NetworkLink derived from feature. Thanks so much!!

Coordinator
Jun 7, 2011 at 10:42 PM

You raise a good point; there are a lot of classes in the library so it's not always obvious what they inherit.

As I'm just a hobby developer I only have the express edition of Visual Studio so can't generate a class diagram, but there is a basic diagram on the KML Reference page (I've updated the Documentation page of this project to include a link to it too); if I get time I'll look at updating the documentation to include a class diagram of this library to make it easier in the future.

Sam

Jun 7, 2011 at 11:47 PM

Ok. So far so good.

 

One more thing, can you point me to documentation on using your StyleUrl type? No matter what I do I cannot get a properly formatted URI created to feed it. I expect to be able to provide #whatever. That's is not a URI. Is your type wrong?

 

See iconstyle element here: http://code.google.com/apis/kml/documentation/kmlreference.html#iconstyle

Coordinator
Jun 8, 2011 at 3:52 AM

The Uri class used in the library is the one provided by the .NET framework. I think the problem you might be having is that by default it will try to create an absolute reference, however, there is an overloaded constructor which should help:

IconStyle iconStyle = new IconStyle();
iconStyle.Icon = new IconStyle.IconLink(new Uri("#whatever", UriKind.Relative));