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

text in shapes

2 Answers 116 Views
Map
This is a migrated thread and some comments may be shown as answers.
Donna
Top achievements
Rank 1
Donna asked on 27 Jun 2011, 08:01 PM
I'm using an empty provider, so I'm not getting any state/country names in my maps.  Is there a way to add text to each shape in the shapefiles so I can get the state names to show up?  Maybe somehow in the PreviewReadCompleted method?

Donna

2 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 30 Jun 2011, 09:54 AM
Hi Donna,

New CaptionLocation and CaptionTemplate properties which allow to specify shape caption will be available in the 2011.Q2 release.
Currently, you can add captions as TextBlock objects from the code behind using the PreviewReadCompleted event. If your DBF file contains the state name field, then you can use the ExtendedData element for each shape to get the state name. The property of ExtendedData which contains the state name will have the same name as the field in the DBF file. Also, I would recommend to add new latitude and longitude fields (for example "LABEL_LAT" and "LABEL_LON") to the DBF file for specifying the location of state name label.
The sample code is the following:
<telerik:RadMap x:Name="RadMap1" Center="40,-100" ZoomLevel="3">
    <telerik:RadMap.Provider>
        <telerik:EmptyProvider />
    </telerik:RadMap.Provider>
    <telerik:InformationLayer x:Name="stateLayer">
        <telerik:InformationLayer.Reader>
            <telerik:MapShapeReader Source="usa_states.shp" DataSource="usa_states.dbf" PreviewReadCompleted="MapShapeReader_PreviewReadCompleted"/>
        </telerik:InformationLayer.Reader>
    </telerik:InformationLayer>
    <telerik:InformationLayer x:Name="labelLayer" />
</telerik:RadMap>

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }
 
    private void MapShapeReader_PreviewReadCompleted(object sender, PreviewReadShapesCompletedEventArgs eventArgs)
    {
        if (eventArgs.Error == null)
        {
            HotSpot hotSpot = new HotSpot();
            hotSpot.X = 0.5;
            hotSpot.Y = 0.5;
 
            foreach (MapShape shape in eventArgs.Items)
            {
                TextBlock label = new TextBlock();
 
                string stateName = (string)shape.ExtendedData.GetValue("STATE_NAME");
                label.Text = stateName;
 
                double latitude = (double)shape.ExtendedData.GetValue("LABEL_LAT");
                double longitude = (double)shape.ExtendedData.GetValue("LABEL_LON");
                Location location = new Location(latitude, longitude);
                MapLayer.SetLocation(label, location);
 
                MapLayer.SetHotSpot(label, hotSpot);
 
                this.labelLayer.Items.Add(label);
            }
        }
    }
}

All the best,
Andrey Murzov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Donna
Top achievements
Rank 1
answered on 30 Jun 2011, 03:41 PM
Thank You!  I was able to do this by using each shape's GeographicalBounds Center Latitude and Longitude.

HotSpot
hotSpot = new HotSpot(); 
hotSpot.X = 0.5;
hotSpot.Y = 0.5;
  
foreach (MapShape shape in eventArgs.Items) 
{
     state = (string)shape.ExtendedData.GetValue(currentPropertyName); 
     centerLat = shape.GeographicalBounds.Center.Latitude;
     centerLong = shape.GeographicalBounds.Center.Longitude;
  
     TextBlock label = new TextBlock(); 
     label.Text = state;
     label.FontSize = 8;
  
     Location location = new Location(centerLat, centerLong); 
     MapLayer.SetLocation(label, location); 
     MapLayer.SetHotSpot(label, hotSpot); 
     this.labelLayer.Items.Add(label);

 

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