Reimers.dk

.NET, AJAX and Google APIs brought together
Welcome to Reimers.dk Sign in | Join | Help
in Search

Error loading kml polygons

Last post 02-27-2010, 20:25 by jjrdk. 4 replies.
Sort Posts: Previous Next
  •  02-16-2010, 13:01 3014

    Error loading kml polygons

    Hi,

     I am trying to load a kml file with polygon definistions using the following code:

    Reimers.Silverlight.Bing.OverlayManager manager = new Reimers.Silverlight.Bing.OverlayManager(Map1);
    manager.ReadKml(mykml);

     where Map1 is my bing maps silverlight control.

    The sample kml string 'myKml' is below [1].

    I get the following error:

    'Element is already the child of another element' stack trace  is here [2]

     Its driving me nuts! :p I have tried a simpler kml file with a single point which displays just fine. Do you have any ideas as to why this error is being generated? Does your librabry support kml polgon definitions? Am I missing the obvious?

     Thanks for any pointers

     Ben

    [1] sample kml string:

          public string mykml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    <kml xmlns=""http://earth.google.com/kml/2.2"">
      <Document>
        <name><![CDATA[US States]]></name>
        <open>1</open>
        <Style id=""Style_5"">
          <IconStyle>
            <scale>0.4</scale>
            <Icon>
              <href>http://maps.google.com/mapfiles/kml/shapes/star.png</href>
            </Icon>
          </IconStyle>
          <LabelStyle>
            <color>9900ffff</color>
            <scale>1</scale>
          </LabelStyle>
          <LineStyle>
            <color>990000ff</color>
            <width>2</width>
          </LineStyle>
          <PolyStyle>
            <color>997f7fff</color>
            <fill>1</fill>
            <outline>1</outline>
          </PolyStyle>
        </Style>
        <Placemark id=""pm269"">
          <name><![CDATA[Connecticut (1788)]]></name>
          <Snippet maxLines=""0"">empty</Snippet>

          <styleUrl>#Style_5</styleUrl>
          <MultiGeometry>

            <Polygon id=""g918"">
              <altitudeMode>clampToGround</altitudeMode>
              <outerBoundaryIs>
                <LinearRing>
                  <coordinates>
                    -73.5303920707175,41.5227455563004
                    -73.5442932536742,41.3652984767995
                    -73.5502596731917,41.2936207043954
                    -73.4781207568954,41.2107548889389
                    -73.7252376566967,41.1003542249946
                    -73.6537237755256,41.0126172825253
                    -73.6531514608995,40.9983924455247
                    -73.1044185740856,41.1610394427248
                    -72.906734323612,41.2700631991856
                    -72.5272448622889,41.2637025875984
                    -72.378152966058,41.2781022275496
                    -72.3784123498461,41.3583482711136
                    -72.3263548978381,41.2896412329135
                    -72.2814159025209,41.2811454708603
                    -71.866678442895,41.3227696452715
                    -71.8477722040922,41.3253484832964
                    -71.8368696812943,41.3419614666217
                    -71.8459956537022,41.4038545416488
                    -71.8027434308056,41.4158290540058
                    -71.7901942031214,41.6013068793249
                    -71.792605218292,41.6417579304637
                    -71.7882488621949,41.7216033953237
                    -71.7978316087619,42.0042748046851
                    -71.8023407165877,42.0179769339478
                    -72.0949717608141,42.0257995069483
                    -72.1363467150764,42.0264020644269
                    -72.5075717905207,42.0307660006011
                    -72.5712258924299,42.0301249737628
                    -72.5819073702202,42.0216068944432
                    -72.6078252730946,42.0228008077559
                    -72.6095266969696,42.030536850941
                    -72.7558938179071,42.0338475230699
                    -72.7675750314897,42.0021671817802
                    -72.817679571843,41.9971850435429
                    -72.8164509949267,42.033507731778
                    -73.0060955098662,42.0360092024926
                    -73.0456324711282,42.0363103821922
                    -73.4842302844536,42.0474280500728
                    -73.5171474133328,41.6656861632365
                    -73.5303920707175,41.5227455563004
                  </coordinates>
                </LinearRing>
              </outerBoundaryIs>
            </Polygon>
          </MultiGeometry>
        </Placemark>
      </Document>
    </kml>
    ";

    [2] stack trace of error:

       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value)
       at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1 collection, DependencyObject value)
       at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value)
       at System.Windows.SetterBaseCollection.AddInternal(SetterBase value)
       at System.Windows.PresentationFrameworkCollection`1.Add(T value)
       at Reimers.Silverlight.Bing.OverlayManager.KmlStyleChanged(Object sender, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object newValue, Object oldValue)
       at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isSetByStyle, Boolean isSetByBuiltInStyle, PropertyInvalidationReason reason)
       at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at Reimers.Silverlight.Bing.OverlayManager.SetKmlStyle(DependencyObject obj, KmlStyle value)
       at Reimers.Silverlight.Bing.OverlayManager.ApplyStyle(MapPolyline line, String styleUrl)
       at Reimers.Silverlight.Bing.OverlayManager.GrabLines(IEnumerable`1 placemarks)
       at Reimers.Silverlight.Bing.OverlayManager.ReadKml(XDocument kml)
       at Reimers.Silverlight.Bing.OverlayManager.ReadKml(String kml)
       at AreaMaps.MainPage..ctor()
       at AreaMaps.App.Application_Startup(Object sender, StartupEventArgs e)
       at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

  •  02-18-2010, 8:53 3016 in reply to 3014

    Re: Error loading kml polygons

    I'm sorry about your problem. I'll have a look at what could be going wrong and get back to you.
  •  02-19-2010, 17:04 3017 in reply to 3014

    Re: Error loading kml polygons

    I found the cause of the problem. The reason it was throwing the error about the element being the child of another is because it was assigning the same style to two different overlays. The reason there were two overlays was because of an error in the KML parser.

    I checked in the fix to the SVN trunk, so you can get it there. Otherwise I will be uploading a new build over the weekend. 

    Filed under:
  •  02-23-2010, 18:46 3028 in reply to 3017

    Re: Error loading kml polygons

    Thats great! Thank you for the speedy fix. Its all displaying nicely!

     I have 2 small queries:

     My map is covered with polgons defining postcode areas as defined from my kml file. The default navigation methods of click and drag don't seem to be allowing me to navigate through/around the map unless I loop through the polygons and make each one editable.

     Secondly how can i get access to the underlying kml data associated with an individual polygon added via the ReadKML method? I can get the locations and assorted style data and can get the data from the tooltip  but need to get at the <description></description> data from the kml for each polgon that has been added to the overlay manger:

      var polys = from ovl in manager
                                 where ovl is Microsoft.Maps.MapControl.Core.MapShapeBase
                                 select ovl;
                 
                    foreach (var overlay in polys)
                    {
                         Microsoft.Maps.MapControl.MapPolygon poly = ((Microsoft.Maps.MapControl.MapPolygon)overlay);
                        //gets the tooltop containing the postcode prefix
                        string tool = ToolTipService.GetToolTip(poly).ToString();
                                         
                        //add pop up window
                        //Reimers.Silverlight.Kml.KmlOptions options = new Reimers.Silverlight.Kml.KmlOptions { Name = "hello", Description = "heres my description \n new line here" };
                        //Reimers.Silverlight.Bing.OverlayManager.SetKmlOptions(overlay, options);
                        //Reimers.Silverlight.Bing.OverlayManager.SetInfoContent(overlay, new Reimers.Silverlight.Controls.InfoWindow { Content = options });
                    }

     I get the feeling I'm being thick here (quite a posibility ;p) Can you point me in the right direction?

     Thanks

    ben

     

  •  02-27-2010, 20:25 3032 in reply to 3028

    Re: Error loading kml polygons

    When parsing the KML file the OverlayManager creates an instance of the KmlOptions class to hold the various KML data (including the description). This object is set as an attached property on the overlay. So you should be able to read it as a normal attached property, i.e. OverlayManager.GetKmlOptions(overlay)
View as RSS news feed in XML