How do you control an OverlayItem's size on the Google Android map?

Go To StackoverFlow.com

1

So I have almost the exact same problem discussed in this post except that the hack job of a solution that they decided to go with simply isn't going to work for me. I need a real solution, so I decided to reverse engineer the android maps source code to find the best solution.

My current solution is to override the draw method in my class that extends OverlayItem, but then I run into the problem that tapping on the overlay item on the map doesn't work (the tap registers at a location other than where the actual item is drawn). So, i'm just going to keep digging until I find the proper mix of methods to override.

2012-04-03 21:27
by Ring


1

For anybody out there that has the same problem as me, the solution is actually quite simple.

All you have to do is set the bounds on the OverlayItem's drawable, like this:

Drawable d = myOverlayItem.getMarker(0);
d.setBounds(-xWidth/2, -yWidth/2, xWidth, yWidth);

The parameter for getMarker actually depends on the state of the overlayItem, but personally for me its the same for all my states so I didn't care and just used 0.

I'm not sure how the solution will vary depending on where you actually set the bounds, but for me I did it within the draw method of my ItemizedOverlay subclass:

@Override
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow) {
    if (!shadow) {
        // Do your projection calculations here, if necessary

        for (int i = 0; i < mOverlays.size(); i++) {
            Drawable d = mOverlays.get(i).getMarker(0);
            d.setBounds(-width/2, -height/2, width/2, height/2);
        }
    }

    super.draw(canvas, mapView, shadow);
}

Its worth noting that the bounds of the overlay item start from 0,0, which actually is a canvas translated to the top left of the OverlayIcon. In my example, if you set width to 50 and height to 50 the OverlayItem would be drawn on the map centered on it's definied position (set in the OverlayItem's constructor). You can see this happening in the source of Overlay:

protected static void drawAt(Canvas canvas, Drawable drawable, int x, int y, boolean shadow)
  {
      // .... Do Stuff ....

      canvas.save();
      canvas.translate(x, y);

      // .... Do Stuff ....

      drawable.draw(canvas);

      // .... Do Stuff ....

      canvas.restore();
  }

(It happens with the canvas.translate call)

On the off chance that somebody cares here's the full source for Overlay.

(It took me a lot of work to reverse engineer maps.jar so I might as well get the most out of my effort).

2012-04-04 01:47
by Ring