This is a migrated thread and some comments may be shown as answers.

KML style data and dynamic poly color

4 Answers 291 Views
Map
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 07 Jan 2010, 11:09 PM
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).

4 Answers, 1 is accepted

Sort by
0
Accepted
Andrey
Telerik team
answered on 11 Jan 2010, 04:46 PM
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.
0
Adam
Top achievements
Rank 1
answered on 12 Jan 2010, 11:12 PM
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...

0
Accepted
Andrey
Telerik team
answered on 14 Jan 2010, 04:17 PM
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.
0
Adam
Top achievements
Rank 1
answered on 14 Jan 2010, 06:11 PM
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.




Tags
Map
Asked by
Adam
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Adam
Top achievements
Rank 1
Share this question
or