How to create a StyleMap tag with SharpKml?

2.6k Views Asked by At

I'd like to know how to create the following XML using SharpKml:

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

I've tried several things, but with no success. This is what I have so far:

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;
}

Who knows on how to achieve this?

2

There are 2 best solutions below

0
On

Martijin added the answer to his own question, which is amazing and helped me get to my solution. Just for an alternative that I believe is slightly more generic I figured I'd drop my solution that I got to thanks to Martijin's answer.

Note: my solution is generating these for a line object to be generated, however this could be used for other styles easily

Creating the style objects for a highlight or normal state. The objects made here are what will be added to the stylemap

Here we just pass in the placemark itself (with some details such as the placemark name, etc., available) and a boolean value as to whether it is a highlight or normal style. My usage is thus:

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

Method:

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;
}

Now we create the style selector to be added to the placemark object

Here I pass two style objects and the original placemark object to create the full style map. This is returned to the placemark with a call such as:

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

Method:

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;
}

Why I have kmlDom, kmlBase, and kmlEngine in front of my object types

** My usings are as follows to separate sharpkml objects from my own internal objects

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

I've find the answer to my own question:

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;
}