KML style data and dynamic poly color

5 posts, 2 answers
  1. Adam
    Adam avatar
    28 posts
    Member since:
    Jun 2009

    Posted 07 Jan 2010 Link to this post

    Hi there, sort of a 2 part question here.
    Regarding KML data imports, i haven't been able to get Placemark <PolyStyle> elements to work. It's seems <styleUrl>'s work but manual <style> tags to not. Take for example:

      <Style id="defaultStyle"
        <LineStyle> 
          <color>ffffffff</color> 
          <width>2</width> 
        </LineStyle> 
        <PolyStyle> 
          <color>99ff6600</color> 
          <outline>1</outline> 
          <fill>1</fill> 
        </PolyStyle> 
      </Style> 
      <Placemark> 
        <Style> 
          <LineStyle> 
            <color>ffffffff</color> 
            <width>3</width> 
          </LineStyle> 
          <PolyStyle> 
            <color>99ff6600</color> 
            <outline>1</outline> 
            <fill>1</fill> 
          </PolyStyle> 
        </Style> 
        <name>Name</name> 
        <description>Desc</description> 
        <Polygon> 
        <altitudeMode>clampToGround</altitudeMode> 
        <outerBoundaryIs> 
            <LinearRing> 
                <coordinates> 
                                (Some coordinates defining a region) 
                </coordinates> 
            </LinearRing> 
        </outerBoundaryIs> 
       </Polygon> 
      </Placemark> 
      <Placemark> 
        <styleUrl>#defaultStyle</styleUrl> 
        <name>Name</name> 
         <description>Desc</description> 
         <Polygon> 
        <altitudeMode>clampToGround</altitudeMode> 
        <outerBoundaryIs> 
            <LinearRing> 
                <coordinates> 
                    (some other coordinates) 
                </coordinates> 
            </LinearRing> 
        </outerBoundaryIs> 
          </Polygon> 
       </Placemark> 

    Only the 2nd Placemark actually get a style applied.

    My other related question is once these shapes are imported, is there a way to change their style dynamically. I'm basically trying to import these regions and change their <PolyStyle> so I can change their background fill color on the map (and transparency level if possible).
  2. Answer
    Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 11 Jan 2010 Link to this post

    Hello Adam,

    We're sorry, currently our KML import feature does not support the Style element inside the Placemark.

    The KML reader converts the KML shapes to instances of the MapPinPoint, MapPolyline and MapPolygon class. So, you can change background fill color for imported shapes with using the following sample code:
    List<FrameworkElement> list = KmlReader.Read(stream);
      
    foreach (FrameworkElement element in list)
    {
        MapPolygon shape = element as MapPolygon;
        if (shape != null)
        {
            shape.Fill = new SolidColorBrush(Color.FromArgb(99, 99, 0, 99));
        }
    }
      
    foreach (FrameworkElement shape in list)
    {
        this.informationLayer.Items.Add(shape);
    }

    Greetings,
    Andrey Murzov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Adam
    Adam avatar
    28 posts
    Member since:
    Jun 2009

    Posted 12 Jan 2010 Link to this post

    Thanks for the response Andery.

    Are any of the KML attributes (besides coordinates) preserved as a MapPolygon? In particular the name or description elemets...

    I'm looking for a way to identify the MapPolygons after they have been added to the information layer items.

    foreach (FrameworkElement ele in this.InformationLayer.Items) 
                    { 
                        MapPolygon shape = ele as MapPolygon; 
                        if (shape != null
                        { 
                            if (shape.Name == "SomeRegion"
                            { 
                                shape.Fill = new SolidColorBrush(Color.FromArgb(99, 99, 0, 99)); 
                            } 
                            else 
                            { 
                                shape.Fill = new SolidColorBrush(Color.FromArgb(99, 00, 99, 99)); 
                            } 
                        } 
                    } 

    But the .Name property is blank...

  4. Answer
    Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 14 Jan 2010 Link to this post

    Hi Adam,

    The KML import does not set the name of Placemark to the Name property of MapPolygon object.
    You can use first coordinate or index to identify the MapPolygons that should have custom background.

    <Placemark>
     <name>polygon</name>
     <Polygon>
       <outerBoundaryIs>
       <LinearRing>
         <coordinates>
           -104.940372161415,39.7760251822507
           ...
         </coordinates>
       </LinearRing>
       </outerBoundaryIs>
     </Polygon>
    </Placemark>

    var location = new Location(39.7760251822507, -104.940372161415);
    foreach (FrameworkElement ele in this.informationLayer.Items)
    {
        MapPolygon shape = ele as MapPolygon;
        if (shape != null)
        {
            if (shape.Points[0].Equals(location))
            {
                shape.Fill = new SolidColorBrush(Color.FromArgb(99, 99, 0, 99));
            }
            else
            {
                shape.Fill = new SolidColorBrush(Color.FromArgb(99, 00, 99, 99));
            }
        }
    }

    var polygons = new Collection<MapPolygon>();
    foreach (FrameworkElement ele in this.informationLayer.Items)
    {
        MapPolygon shape = ele as MapPolygon;
        if (shape != null)
        {
            polygons.Add(shape);
            shape.Fill = new SolidColorBrush(Color.FromArgb(99, 00, 99, 99));
        }
    }
      
    polygons[0].Fill = new SolidColorBrush(Color.FromArgb(99, 99, 0, 99));


    Kind regards,
    Andrey Murzov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Adam
    Adam avatar
    28 posts
    Member since:
    Jun 2009

    Posted 14 Jan 2010 Link to this post

    Thanks again for the help Andrey, I'll keep this method of coordinate matching to MapPolygon's in mind for the future.
    In my particular use case there were too many MapPolygon's to create a matching FirstPoint manually. Additionally most of them had overlapping points (often literally the same points for side-to-side region borders) so they were not uniquely identified just by by just 1 or a few sample points.
    As a workaround I created a custom KML importer which preserved the name element for each Placemark inside the name property of the MapPolygons. 

    Thanks again for the help.




Back to Top