Select to view content in your preferred language

Placing label inside a polygon

3182
3
04-11-2013 06:20 PM
Labels (1)
StanleyHo
New Contributor III
Hi,

I have multiples polygons (all rectangles) in a map.
I used SimpleFillSymbol to draw the polygon as it allows me to have different color for the border and fill.
However, I wasn't able to place labels in the polygon.

Is there any way to achieve the following?
- drawing the polygon with a color
- filling the polygon with another different color
- placing a label in the center of the polygon

thanks
0 Kudos
3 Replies
AaronHigh
New Contributor III
Hi,

You could try the following to get a filled polygon with a centered label:

1) Create a polygon fill using a SimpleFillSymbol with the BorderBrush property set to the outline you wish to have and the Fill property set to the secondary color to fill the polygon with.

2) Assuming your rectangle is an ESRI.ArcGIS.Client.Geometry.Polygon object, acquire the extent ESRI.ArcGIS.Client.Geometry.Envelope object and then call the GetCenter() method to get the MapPoint that represents the center of the envelope.

3) Create an ESRI.ArcGIS.Client.Symbols.TextSymbol with geometry set at the centroid of the envelope acquired in step 2 and text poperties you choose.

void labeledPolygon(ESRI.ArcGIS.Client.Geometry.Polygon inPolygon)
{
    Graphic polygonGraphic = new Graphic();
    SimpleFillSymbol fillSymbol = new SimpleFillSymbol()
    {
           BorderBrush = borderBrush,
           Fill = fillBrush
     };
     polygonGraphic.Symbol = fillSymbol;
     polygonGraphic.Geometry = inPolygon;

     Envelope extentEnvelope = inPolygon.Extent;
     MapPoint centerPoint = extentEnvelope.GetCenter();

     Graphic textGraphic = new Graphic();
     TextSymbol textSymbol = new TextSymbol()
      {
           //Text properties
      };
      textGraphic.Symbol = textSymbol;
      textGraphic.Geometry = centerPoint;
      //Add the two graphics to the map.
}


It may be necessary to measure your string and offset by the length to effectively "center" the text.
0 Kudos
DavidMartin
Occasional Contributor II
If you were happy with a map service, then all the rendering and labelling would be done on the server and your WPF app simply displays the resulting image. If your data is static, you could even pre-prepare annotation layers to speed things up. Neither need preclude your providing the data as a feature service as well, but it depends what you're doing with it. If the data is highly dynamic and/or you are editing the data in the WPF app, then you'd want to think carefully about how such an implementation might (not?) work.

Alternatively, would map-tips serve your needs? (http://resources.arcgis.com/en/help/runtime-wpf/concepts/index.html#/Adding_MapTips/0170000000330000...)
0 Kudos
StanleyHo
New Contributor III
Thanks for all the replies.

ahigh2:
I tried your method of doing it. It works nicely but I have a problem. If I will to shift the polygon or do some transformation, the label just stays there. I think the reason to it is its a different graphic from the GraphicsLayer perspectives.

helyxsisltd:
I don't think I can use a map service as my data is dynamic. Users can create and edit the polygons on the maps.
0 Kudos