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

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

by oland :: Rate this Message:

| View in Thread

Hi Don

I got bitten by the fact that my location cluster is a BasicLocation which is implies that the OMText is decluttered so I had to do:

    @Override
    public boolean generate(Projection proj, DeclutterMatrix declutterMatrix) {
        return generate(proj);
    }

In the picture below the magenta rects are the center(s) of my calculation. It seems nearly correct for two digits but not for single digits.

                int height = g.getFontMetrics().getAscent();
                Rectangle2D strBounds = fm.getStringBounds(label.getData(), g);
                double cX = label.getMapLocation().getX()
                        + strBounds.getWidth() / 2;
                double cY = label.getMapLocation().getY() + ((height / 2) - 2);
                int w = 4;
                int h = 4;
                rec.setLocation((int) cX - w / 2, (int) cY - h / 2, (int) cX
                        + w / 2, (int) cY + h / 2);
                rec.setVisible(clustered);
                setLocationMarker(rec);

Is there anything else in Location/BasicLocation that alters the position of the OMText during generate?

BR
Carsten

On 10/28/2011 11:31 PM, Don Dietrick wrote:
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?