[英]How to create a StyleMap tag with SharpKml?
我想知道如何使用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;
}
誰知道如何實現這一目標?
我找到了自己問題的答案:
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;
}
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;
}
**我的使用如下將sharpkml對象與我自己的內部對象分開
using kmlBase = SharpKml.Base;
using kmlDom = SharpKml.Dom;
using kmlEngine = SharpKml.Engine;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.