Placemark Icon not getting displayed in Google Earth

Feb 28, 2016 at 9:38 PM
Edited Feb 28, 2016 at 9:49 PM
I am trying to generate a kml file with list of placmarks generated based on the list of lat, long provided. Each lat, long corresponds to a placemark. I am using google chart api to generate a marker with custom color. Color is based on the value I receive. So it is kind of an heatmap, but the representation shows pins on the map.

The generated kml works perfectly on google maps. But when I plot the same kml on google earth the pins are not displayed. I have narrowed down the problem to the new IconStyle.IconLink(new Uri());

This is working
Icon = new IconStyle.IconLink(new Uri("http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=|2B84B7", UriKind.Absolute));
But this is not working as the generated kml includes cdata tag to icon href
private string pinImageUrl = "http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=|{0}";
Icon = new IconStyle.IconLink(new Uri(string.Format(pinImageUrl, "2B84B7"), UriKind.Absolute)),
Here is the complete modified code
  private Document _kmlDocument;
        private string pinImageUrl = "http://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=|{0}";

        public bool CreateKml(string fileName)
        {
            try
            {
                _kmlDocument = new Document
                {
                    Name = "Test Kml 1",
                    Time = new Timestamp { When = DateTime.UtcNow }
                };

                GenerateStyles();

                var placemark = new Placemark
                {
                    Geometry = new SharpKml.Dom.Point { Coordinate = new SharpKml.Base.Vector(45.96822669299998, 7.717927110000019) },
                    Id = "Step-1",
                    Name = "#1",
                    Description = new Description
                    {
                        Text = "Description"
                    },
                    StyleUrl = new Uri("#styleMap1", UriKind.Relative),
                    Time = new Timestamp { When = DateTime.UtcNow }
                };
                _kmlDocument.AddFeature(placemark);

                var mykmlFile = new Kml { Feature = _kmlDocument };
                //mykmlFile.AddNamespacePrefix(KmlNamespaces.GX22Prefix, KmlNamespaces.GX22Namespace);
                //mykmlFile.AddNamespacePrefix(KmlNamespaces.AtomPrefix, KmlNamespaces.AtomNamespace);
                ////mykmlFile.AddNamespacePrefix(KmlNamespaces.Kml22Prefix, KmlNamespaces.Kml22Namespace);

                var mykmlFileCreated = KmlFile.Create(mykmlFile, true);

                using (var stream = File.OpenWrite(fileName + ".kml"))
                {
                    mykmlFileCreated.Save(stream);
                }
            }
            catch (Exception ex)
            {
                Logger.Write($"CreateKml error {ex}");
                return false;
            }

            return true;
        }

        private void GenerateStyles()
        {
            var color = new Color32(1, 183, 132, 43);
            var pinColor = color.Red.ToString("X2") + color.Green.ToString("X2") + color.Blue.ToString("X2");

            var styleMap = new StyleMapCollection { Id = $"styleMap1" };
            var nPair = new Pair
            {
                State = StyleState.Normal,
                StyleUrl = new Uri($"#style1-normal", UriKind.Relative)
            };
            styleMap.Add(nPair);
            _kmlDocument.AddStyle(styleMap);

            var style = new Style
            {
                Id = "style1-normal",
                Icon = new IconStyle
                {
                    Icon = new IconStyle.IconLink(new Uri(string.Format(pinImageUrl, pinColor), UriKind.Absolute)),
                    Color = color,
                    Scale = 2,
                },
            };
            _kmlDocument.AddStyle(style);

            #region default Style
            var defaultStyleMap = new StyleMapCollection { Id = "styleMap-null" };
            var normalPair = new Pair
            {
                State = StyleState.Normal,
                StyleUrl = new Uri("#style-null-normal", UriKind.Relative)
            };
            defaultStyleMap.Add(normalPair);

            //Add a default style for normal state if there is no Rssi found
            var defaultNStyle = new Style
            {
                Id = "style-null-normal",
                Icon = new IconStyle
                {
                    Icon = new IconStyle.IconLink(new Uri(string.Format(pinImageUrl, "151515"), UriKind.Absolute)),
                    Scale = 1,
                    //Hotspot = new Hotspot { X = 16, Y = 31, XUnits = Unit.Pixel, YUnits = Unit.InsetPixel }
                },
                Label = new LabelStyle
                {
                    Color = new SharpKml.Base.Color32(1, 220, 220, 220),
                    Scale = 1
                }
            };

            _kmlDocument.AddStyle(defaultNStyle);
            _kmlDocument.AddStyle(defaultStyleMap);

            #endregion
        }
Can anybody tell me where is the problem. I am pulling my hair on this. Also, if anybody got any better idea of displaying custom pin image based on the color please suggest. Remember that the kml is generated when the system is offline i.e. no internet. Also, I don't want to host the static images somewhere.

Is it possible to display a think line or a polygon with custom color as a pin on the map?

Thanks.