This project has moved and is read-only. For the latest updates, please go here.

How to get color from a Ploygon's Style

Mar 16, 2016 at 12:31 PM
Once I got the Kml file loaded how do I get the Style that is connected to a Polygon and how do I retrieve the color (area color)?
Mar 21, 2016 at 4:29 PM
Edited Mar 22, 2016 at 10:42 AM
I found a way. This snippet does what I need:
Assuming polygon points to a Polygon node in the KML DOM.
StyleMapCollection pStyle = kmlFile.FindStyle(polygon.GetParent<Placemark>().StyleUrl.ToString().Replace("#", string.Empty)) as StyleMapCollection;
foreach (Pair p in pStyle)
    if(p.State.ToString().ToLower() == "normal")
    Style styleFinally = kmlFile.FindStyle(p.StyleUrl.ToString().Replace("#", string.Empty)) as Style;
    SharpKml.Base.Color32 __col = (styleFinally.Polygon.Color != null) ? styleFinally.Polygon.Color.Value : new SharpKml.Base.Color32(255, 255, 255, 255);
I'm sure this is not the best practice as KML files wrap styles in all possible DOM structures; for instance, this snippet works if styles are given in a StyleMap. If they are given as lonely nodes, this won't work.
So if someone has a better, more general way, please share.