WARNING: This server is unstable and will be retired in the next days. If you want to keep this forum available, please request immediately a migration on the Nabble Support forum. Forums that don't receive any migration request will be deleted forever.

 « Return to Thread: Finding the center (xy) of a OMText?

Re: Finding the center (xy) of a OMText?

by Don Dietrick-2 :: Rate this Message:

| View in Thread

Hi Carsten,

Are you using center justification for the label?  If you draw the poly bounds (I think it shows if matting is turned on), do the bounds look right, even for one character?

I thought polybounds took font and size into account, but now that you mention it, I'm not sure if I've looked at it with one character, which seems to be quite pertinent to you.

- Don

On Thu, Oct 27, 2011 at 10:01 AM, "Carsten Ø. Madsen" <com@...> wrote:
Hello

I'm trying to implement clustering of locations a'la google maps marker clustering. But I'm having difficulties finding the exact center (xy) of the OMText which represents the clustering count. This causes a slight misalignment of the icons sitting behind the count.

Here is my code from generate:

        label.generate(proj);
        label.prepareForRender(g);
        if (label.getPolyBounds() != null) {
            Rectangle rect = label.getPolyBounds().getBounds();
            rh = (int) ((float) (bounds.getHeight() - currentFontDescent / 2));
            rw = (int) rect.getWidth();
            int width = raster.getImage().getWidth(null);
            int height = raster.getImage().getHeight(null);
            raster.setRenderType(OMGraphicConstants.RENDERTYPE_XY);
            double cX = label.getMapLocation().getX() + rw / 2;
            double cY = label.getMapLocation().getY() + currentFontDescent / 2;
            raster.setX((int) (cX - width / 2));
            raster.setY((int) (cY - height / 2));

I tried with getCenterX/Y on getPolyBounds but the result varies with the font size and length of the string being rendered. It seems to work best with string length > 1.

I would like to use

FontMetrics metrics = g.getFontMetrics();
Rectangle2D rect = metrics.getStringBounds(text, g);

but I guess this does not work with OMText and the way it does text rendering.

Any hints?

Thanks
Carsten



 « Return to Thread: Finding the center (xy) of a OMText?