text in shapes

3 posts, 0 answers
  1. Donna
    Donna avatar
    12 posts
    Member since:
    Dec 2010

    Posted 27 Jun 2011 Link to this post

    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. Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 30 Jun 2011 Link to this post

    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
  3. DevCraft banner
  4. Donna
    Donna avatar
    12 posts
    Member since:
    Dec 2010

    Posted 30 Jun 2011 Link to this post

    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);

     

Back to Top