private void GetStyleMapAsync(XElement styleMap, XDocument xDoc, ICredentials credentials, Action<KMLStyle> callback, X509Certificate clientCertificate = null)
{
XNamespace kmlNS = styleMap.Name.Namespace;
KMLStyle kmlStyle = null;
foreach (XElement pair in styleMap.Descendants(kmlNS + "Pair"))
{
XElement key = pair.Element(kmlNS + "key");
if (key != null)
{
if (key.Value == "normal")
{
XElement style = pair.Element(kmlNS + "Style");
if (style != null)
{
kmlStyle = new KMLStyle();
GetStyle(style, kmlStyle);
}
else
{
XElement styleUrl = pair.Element(kmlNS + "styleUrl");
if (styleUrl != null)
{
XAttribute styleIdAttribute = styleMap.Attribute("id");
string styleId = styleIdAttribute == null ? null : styleIdAttribute.Value;
// Get the style from the styleUrl. This may need to downloading an external KML file
GetStyleUrlAsync(styleUrl.Value, xDoc, credentials
, kmlstyle =>
{
//// After obtaining the style (which may have involved recursion and downloading external KML files
//// to resolve style URLs) be sure to always overwrite the styleId with the name given to this StyleMap.
if (styleId != null && kmlstyle != null)
kmlstyle.StyleId = styleId;
callback(kmlstyle);
}, clientCertificate);
return;
}
}
}
}
}
// execute the callback with the found style (or null if not found)
callback(kmlStyle);
}