1

我想使用 Sharpkml 在文件夹中插入图标​​并得到这样的结果 klm:

        <?xml version="1.0" encoding="utf-8"?>
        <kml xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns="http://www.opengis.net/kml/2.2">
         <Document>
          <name>Doc</name>
          <Folder>
            <name>fol1</name>
              <Icon>
                <href>green.png</href>
               </Icon>
           </Folder>
        </Document>
        </kml>

但是找不到办法。你可以帮帮我吗?

Kml kml = new Kml(); 
kml.AddNamespacePrefix(KmlNamespaces.GX22Prefix, KmlNamespaces.GX22Namespace); 
var document = new Document(); 
document.Name = "Doc"; 
kml.Feature = document; 
var folder1 = new Folder(); 
folder1.Name = "fol1"; 
document.AddFeature(folder1);
4

1 回答 1

1

找到一种变体,它可能与 ListStyle 有关。

        Kml kml = new Kml();

        kml.AddNamespacePrefix(KmlNamespaces.GX22Prefix, KmlNamespaces.GX22Namespace);

        var document = new Document();
        document.Name = "Doc";
        kml.Feature = document;

        var style_f = new Style();
        style_f.Id = "fol";

        var liststyle_f = new ListStyle();
        var itemicon_f = new ItemIcon();

        itemicon_f.State = ItemIconStates.Open | ItemIconStates.Closed;
        itemicon_f.Href = new Uri("green.png", UriKind.Relative);

        liststyle_f.AddItemIcon(itemicon_f);
        style_f.List = liststyle_f;

        document.AddStyle(style_f);

        var folder1 = new Folder();
        folder1.Name = "fol1";
        folder1.StyleUrl = new Uri("#fol", UriKind.Relative);

        document.AddFeature(folder1);

        var kml_file = @"C:\Users\user\Documents\kml_file.kml";

        KmlFile kmlfile = KmlFile.Create(kml, false);
        using (var stream = System.IO.File.OpenWrite(kml_file))
        {
            kmlfile.Save(stream);
        }

结果:

    <?xml version="1.0" encoding="utf-8"?>
    <kml xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns="http://www.opengis.net/kml/2.2">
      <Document>
        <name>Doc</name>
        <Style id="fol">
          <ListStyle>
            <ItemIcon>
              <state>open closed</state>
              <href>green.png</href>
            </ItemIcon>
          </ListStyle>
        </Style>
        <Folder>
          <name>fol1</name>
          <styleUrl>#fol</styleUrl>
        </Folder>
      </Document>
    </kml>
于 2019-11-19T17:26:37.403 回答