Select to view content in your preferred language

Adding labels to a GraphicsLayer (API version 3)

4824
6
05-27-2016 03:05 AM
BenjaminAbbott
Occasional Contributor

I'd like to arbitrarily label elements within a GraphicsLayer, using the appropriate label-position values from the client-side labeling engine. This would need to be done per-graphic, as this GraphicsLayer can contain multiple geometry types. There are a number of posts on this, but none have an appropriate solution.

A member of ESRI staff posted an example which worked under API 3.9, but is broken in subsequent releases:

https://community.esri.com/message/381496#comment-381496

Other solutions involve adding extra FeatureLayers to then either assign to LabelLayer (which is deprecated), or use the now preferred labeling capabilities of the FeatureLayer itself.

Our use case: we use a GraphicsLayer to manage a "saved items" panel on our fairly sophisticated JSAPI-v3-driven web GIS. Our users want to be able to label graphics without the hack method if adding a TextSymbol "somewhere underneath the blob". This is horrific, and our users often end up with junk print outputs. I've experimented with running labelled FeatureLayers for each geometry type in the background, but this too is horribly compromised, and adds a lot of processing overhead to an already complex application.

Any suggestions appreciated.

Tags (2)
0 Kudos
6 Replies
PanagiotisPapadopoulos
Esri Regular Contributor

you can use a graphic layer only for the Text Symbols.

Use this graphic layer on top of all other graphic layers in order to avoid any visibility problems.

Capture.PNG

Text Symbol allow you to edit all properties (color, text size, position, shadow) from user interface.

Capture1.PNG

graphic layer can be exported as Json file in order to save it on file or database for future use.

Also you can take advantage from Browser's Local storage.

BenjaminAbbott
Occasional Contributor

Thanks for your reply. That's not really what I'm looking for though. I'd like to leverage the inbuilt alignment properties of the LabelClass, because:

1. Spatially positioned text labels don't work at multiple scales (especially a problem when printing), and

2. Our interface is crowded enough as it is. The audience for this tool are not GIS professionals, so I need a generic solution which requires little to no thought from the user.

They need to be able to check a box which says "Label this", and the system needs to make a good, educated guess as to how to label the graphic. We do that with Dynamic Layers on server-side & it works remarkably well. Unfortunately, users cannot label their drawn graphics without resorting to a new graphic of type TextSymbol, and spatial positioning.

We do use local storage for a lot of this stuff actually, but my performance concerns are based around adding more and more layers into memory. We have some Citrix clients, and this is a real problem for them.

0 Kudos
FC_Basson
MVP Regular Contributor

Would it be an option to create an additional Map Service that only shows the label and no colorsymbology for the feature layer and then show that layer based on a layer definition?  That way the label positioning is also dynamic with the standard labelling engine.

0 Kudos
BenjaminAbbott
Occasional Contributor

That would work where the graphic was a saved version of an existing feature from a known feature layer - my question is more targeted toward the labeling of graphics which users draw themselves.

I have a proof of concept which uses a (programmatically created) feature layer which can sit below the graphics layer and generate appropriate labels, but I haven't found a way to suppress the symbol for this feature layer completely. Therefore, showing and hiding of graphics in the "saved items" list is broken because the underlying symbol is always visible.

We also give users an interface to change the colour, size and shape of point features, for example, so if the label is attached to a symbol in a feature layer below with its own size, and the user increases the size of their graphic, the offset of the label in the underlying feature layer will be incorrect.

I don't think using a feature layer to actually hold the user-drawn graphics is an option because they will all potentially have different symbols.

0 Kudos
FC_Basson
MVP Regular Contributor

So you want to labels to be automatically placed?  We also use something similiar to Panagiotis Papadopoulos​ suggestion where the user can place the label anywhere on the feature and set the label properties such as alignment, colour, size etc and it works pretty well.

BenjaminAbbott
Occasional Contributor

Yes - that's exactly it. I need it to 'just work' from the point of the end user. Manual label positioning isn't an option.

We could potentially make this work, the only issue with the solution above is the label is positioned relative to the absolute centre point of the graphic, and not relative to the edge of the symbol (as it would with a labeled feature layer). A little fudging would be required, but a lot less than managing a bunch of synchronous feature layers.

Much appreciated both.

0 Kudos