Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the shadow of my MapView Overlay in the wrong place?

Tags:

android

I'm just trying to display an icon of a man with a circle under his feet in the center of the MapView. Here's my Overlay code:

public class CenterOverlay extends Overlay
{
    private Drawable    d;

    public CenterOverlay(Drawable drawable)
    {
        final int w = drawable.getIntrinsicWidth();
        final int h = drawable.getIntrinsicHeight();
        drawable.setBounds(0, 0, w, h);
        this.d = drawable;
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
    {
        final int mapCenterX = mapView.getWidth() / 2;
        final int mapCenterY = mapView.getHeight() / 2;
        final int x = mapCenterX - d.getIntrinsicWidth() / 2;
        final int y = mapCenterY - d.getIntrinsicHeight();
        drawAt(canvas, d, x, y, shadow);
    }
}

The code to add the Overlay to the MapView is working fine (I can see the icon right where it's supposed to be). The problem is that the automagically generated shadow is in the wrong spot (about the icon's width to the left, and about halfway up the icon).

Thanks in advance for your help!

like image 647
Ben Barbour Avatar asked Jun 24 '10 22:06

Ben Barbour


1 Answers

With the bounds you're setting -- 0, 0, w, h -- the origin is at the top-left of the icon, which is most likely what's causing the incorrect shadow calculation. I don't know the details of this image, but if it's an icon of a man, you likely want the origin near the bottom center. ItemizedOverlay.boundCenterBottom() can do this for you, or if you want more fine-grained control, you can try playing with code like this:

drawable.setBounds(-width / 2, -height, width - (width / 2), 0)
like image 52
Steve Avatar answered Nov 16 '22 10:11

Steve