【问题标题】:How to create a StyleMap tag with SharpKml?如何使用 SharpKml 创建 StyleMap 标签?
【发布时间】:2015-02-16 14:51:49
【问题描述】:

我想知道如何使用 SharpKml 创建以下 XML:

<StyleMap id="msn_placemark_circle">
    <Pair>
        <key>normal</key>
        <styleUrl>#sn_placemark_circle</styleUrl>
    </Pair>
    <Pair>
        <key>highlight</key>
        <styleUrl>#sh_placemark_circle_highlight</styleUrl>
    </Pair>
</StyleMap>

我尝试了几件事,但没有成功。这是我目前所拥有的:

public static StyleSelector Generate_M_ylw_pushpin3()
{
    var stylemap = new StyleMapCollection();
    stylemap.Id = "s_ylw-pushpin3";
    var normalPair = new Pair();
    normalPair.Id = "normal";
    normalPair.Selector = StyleGenerator.Generate_s_ylw_pushpin_hl3();
    //normalPair.StyleUrl = new Uri(#sh_placemark_circle_highlight); // Exception by .NET

    var highlightPair = new Pair();
    highlightPair.Id = "highlight";
    highlightPair.Selector = StyleGenerator.Generate_s_ylw_pushpin_hl3();
    //highlightPair.StyleUrl = new Uri(#sh_placemark_circle_highlight); // Exception by .NET

    stylemap.Add(normalPair);
    stylemap.Add(highlightPair);

    return stylemap;
}

// This code just works fine
public static StyleSelector Generate_s_ylw_pushpin_hl3()
{
    var style = new Style();
    style.Id = "s_ylw-pushpin_hl3";
    var iconStyle = new IconStyle();
    iconStyle.Color = Color32.Parse("ff00ff00");
    iconStyle.Scale = 1.18182;
    iconStyle.Icon = new IconStyle.IconLink(new Uri("http://some/url"));
    var labelStyle = new LabelStyle();
    labelStyle.Color = Color32.Parse("00ffffff");

    style.Icon = iconStyle;
    style.Label = labelStyle;

    return style;
}

谁知道如何做到这一点?

【问题讨论】:

    标签: c# kml sharpkml


    【解决方案1】:

    我找到了自己问题的答案:

    public static StyleSelector Generate_M_ylw_pushpin3()
    {
        var stylemap = new StyleMapCollection();
        stylemap.Id = "s_ylw-pushpin3";
        var normalPair = new Pair();
        normalPair.StyleUrl = new Uri("#sh_placemark_circle", UriKind.Relative); 
        normalPair.State = StyleState.Normal;
    
        var highlightPair = new Pair();
        highlightPair.StyleUrl = new Uri("#sh_placemark_circle_highlight", UriKind.Relative); 
        highlightPair.State = StyleState.Highlight;
    
        stylemap.Add(normalPair);
        stylemap.Add(highlightPair);
    
        return stylemap;
    }
    

    【讨论】:

    • 感谢您发布您的答案,绝对帮助我解决了我的问题。我也添加了我的解决方案,只是为了帮助其他有类似问题的人
    【解决方案2】:

    Martijin 为他自己的问题添加了答案,这太棒了,帮助我找到了解决方案。只是为了一个我认为更通用的替代方案,我想我会放弃我的解决方案,感谢Martijin的回答。

    注意:我的解决方案是为要生成的线对象生成这些,但是这可以很容易地用于其他样式

    为高亮或正常状态创建样式对象。这里制作的对象将被添加到样式图中

    这里我们只传入地标本身(以及一些可用的地标名称等详细信息)和一个布尔值,以确定它是高亮样式还是普通样式。我的用法是这样的:

    kmlDom.Style normalStyle = createPlacemarkLineStyle(thisPlacemark, false);
    kmlDom.Style highlightStyle = createPlacemarkLineStyle(thisPlacemark, true);
    

    方法:

    public kmlDom.Style createPlacemarkLineStyle ( kmlDom.Placemark placemark , bool highlight )
    {
      kmlDom.Style styleNode = new kmlDom.Style( );
      // Add Line Style
      kmlDom.LineStyle lineStyle = new kmlDom.LineStyle( );
      if( !highlight )
      {
        styleNode.Id = String.Format( "{0}-normal", placemark.placemarkName );
        lineStyle.Color = hexToColor("ff0000ff");
        lineStyle.Width = 2;
      }
      else
      {
      styleNode.Id = String.Format( "{0}-highlight", placemark.placemarkName );
        lineStyle.Color = hexToColor( "ff0000ff" );
        lineStyle.Width = 2;
      }
      styleNode.Line = lineStyle;
      return styleNode;
    }
    

    现在我们创建要添加到地标对象的样式选择器

    这里我传递了两个样式对象和原始地标对象来创建完整的样式地图。这通过如下调用返回到地标:

    thisPlacemark.StyleSelector = createPlacemarkLineStyleMap(placemark, normalStyle, highlightStyle);
    

    方法:

    public kmlDom.StyleSelector createPlacemarkLineStyleMap ( kmlDom.Placemark  placemark , kmlDom.Style normalStyle , kmlDom.Style highlightStyle )
    {
      // Set up style map
      kmlDom.StyleMapCollection styleMapCollection = new kmlDom.StyleMapCollection( );
      styleMapCollection.Id = String.Format( "{0}-stylemap" , placemark.placemarkName );
      // Create the normal line pair
      kmlDom.Pair normalPair = new kmlDom.Pair();
      normalPair.StyleUrl = new Uri(String.Format("#{0}", normalStyle.Id), UriKind.Relative);
      normalPair.State = kmlDom.StyleState.Normal;
      // Create the highlight line pair
      kmlDom.Pair highlightPair = new kmlDom.Pair( );
      highlightPair.StyleUrl = new Uri( String.Format( "#{0}" , highlightStyle.Id ) , UriKind.Relative );
      highlightPair.State = kmlDom.StyleState.Highlight;
      // Attach both pairs to the map
      styleMapCollection.Add( normalPair);
      styleMapCollection.Add( highlightPair );
    
      return styleMapCollection;
    }
    

    为什么我的对象类型前面有 kmlDom、kmlBase 和 kmlEngine

    ** 我的使用方法如下,将sharkmml对象与我自己的内部对象分开

    using kmlBase = SharpKml.Base;
    using kmlDom = SharpKml.Dom;
    using kmlEngine = SharpKml.Engine;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 2021-12-03
      • 2019-10-20
      • 2021-08-25
      • 2017-03-13
      • 1970-01-01
      相关资源
      最近更新 更多