Select to view content in your preferred language

Strange label placement with LabelClass

4071
4
06-03-2016 01:33 AM
RaphaelDürst
Deactivated User


Hello

I'm trying to add labels to a FeatureLayer with LabelClass on version 3.16 of the javascript API.

I'm just displaying countries with different colors and trying to add the country name as a label.

Unfortunately, some of the labels are placed at very strange places instead of in the center of the country.

I have provided two examples in the attached screenshots.

This is the code which I am using to add the labels:

var featureLayer = new FeatureLayer(featureLayerPath, {
    outFields: ['iso_a3', 'iso_a2', 'name']
});
featureLayer.setRenderer(this.renderer); // the renderer is defined earlier and works

var labelSymbol = new this.esri.TextSymbol({
    font: new this.esri.Font('10', this.esri.Font.STYLE_NORMAL, this.esri.Font.VARIANT_NORMAL,
        this.esri.Font.WEIGHT_NORMAL, 'Helvetica'),
    color: new this.esri.Color('#000000')
});

var lc = new this.esri.LabelClass({
    labelExpressionInfo: {value: '{name}'},
    labelPlacement: 'center-center'
});
lc.symbol = labelSymbol;

featureLayer.setLabelingInfo([lc]);

map.addLayer(featureLayer);

I have already tried all possible options for the labelPlacement property, but this doesn't change anything.

Does anyone have an idea what I might be doing wrong and how I could do it correctly?

I'm thankful for any help!

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Raphael,

  Can you share your whole code for debugging?

0 Kudos
RaphaelDürst
Deactivated User

Hi Robert,


Thank you for your answer.

I have uploaded an example of my map to our web server:

Map Test

And the code for my map is in this file.

I hope, you can work with that.

Regards,

Raphael

0 Kudos
FC_Basson
MVP Regular Contributor

The API example shows the same behaviour.  Go to Michigan state in the USA and see for yourself.  Your Map Service seems to contain enough information per feature part, even a "homepart" field (which I assume you've tried to use in the where clause for the labelling expression) that might help to define the labelling parts.

RobertScheitlin__GISP
MVP Emeritus

Raphael,

  As each country is a multi-part polygon and not multiple individual polygons, you can not do anything about this issue. If you were to take the polygons and explode them into multiple individual polygons and then assign the homepart of 1 to the largest mass polygon and 0 to the other parts then you could do as FC Basson​ suggests, or use your shapearea calculation for which polygon to label. Right now the JS API will choose which part of the multi-part polygon to label based on proximity to other labels and other unknown criteria.

0 Kudos