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

Add Title To Shape

6 Answers 185 Views
Map
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 10 Mar 2014, 05:32 PM
Is there anyway to add a descriptive text based title to the map shapes when using GEOjson data?  I have a fill color but want to put a text overlay on the shape as well, i.e. the two letter state abbreviation.

6 Answers, 1 is accepted

Sort by
0
T. Tsonev
Telerik team
answered on 12 Mar 2014, 11:19 AM
Hi,

Adding text to shapes is possible with HTML elements. We can attach the labels to the map "scrollElement" which is automatically scrolled with the map layers.

      shapeCreated: function(e) {  
        // Create the label
        var label = $("<span class='label'></div>")
        .html(e.shape.dataItem.properties.abbrev)
        .appendTo(e.sender.scrollElement);        
        
        // Position the label
        ...      
      }


-- Live demo --

Positioning is a bit trickier, as we need to have the bounding box of the shape. We'll add a public method for this saving you a good part of the demo code.

We intend to make this much easier, possibly with built-in templates for the shapes.

I hope this helps.

Regards,
T. Tsonev
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
Joe
Top achievements
Rank 1
answered on 12 Mar 2014, 03:49 PM
This is a great solution thanks!

How would you work this for MultiPolygon shapes to prevent the label from being added more than once, for instance Alaska and Hawaii?
0
T. Tsonev
Telerik team
answered on 13 Mar 2014, 12:08 PM
Hi,

That's a good point. Items bound through the Data Source have unique ID's that we can use for this purpose:

shapeCreated: function(e) {
  if (document.getElementById(e.shape.dataItem.uid)) {
    return;
  }
  ...  
  label.html(e.shape.dataItem.properties.abbrev)


-- Updated demo --

Thank you for your patience.

Regards,
T. Tsonev
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
Yong Yong
Top achievements
Rank 1
answered on 24 Nov 2014, 05:58 AM
Hi, the links for the demo show "Failed to load snippet". Can you fix it? Also, have you added the public methods for that feature?
0
T. Tsonev
Telerik team
answered on 25 Nov 2014, 03:43 PM
Hello,

We've updated the snippet to the latest Q3'14 release.
This simplified the code somewhat, as bounding boxes are now calculated automatically.

We've also published a How To: Add Shape Titles article that accomplishes the same goal without using overlay elements.
This is the recommended solution for the current version.

Regards,
T. Tsonev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Yong Yong
Top achievements
Rank 1
answered on 26 Nov 2014, 08:57 AM
Thanks. That works perfectly.
Tags
Map
Asked by
Joe
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
Joe
Top achievements
Rank 1
Yong Yong
Top achievements
Rank 1
Share this question
or