0

I'm trying to just draw visible OverlayItems, thats why I determine the maps visible rect but I'm not able to determine the Rect where the Canvas will draw the OverlayItem.

Thats what I did till now (method in itemized overlay).. but the getClipBounds() doesn't return the correct Rect

@Override
    public void draw(Canvas canvas, MapView map, boolean shadow) {
        if (getMapBounds().intersect(canvas.getClipBounds())) {
            super.draw(canvas, map, false);
        }
    }

I don't want to draw other OverlayItems, i want to know if my canvas draw something within the visible rects of the map view Because if not i don't draw this canvas This is done to speed up the mapview that has nearly 2000 overlay items

lukaswelte
  • 2,951
  • 1
  • 23
  • 45

2 Answers2

0

If you are looking to draw overlays like these ones:

So basically what you have to do is impose the customImage on the background box with the help of a canvas. Using this implementation you can effectively create a BitmapDrawable from the canvas that you can then assign as a marker for your 'ItemizedOverlay'. If this is the type of itemized Overlays you were looking for then there's no need to override the draw function of the itemized overlay class. All you need to do is use the following code to create a BitmapDrawable that you can assign to your ItemizedOverlay in its constructor. Here's the function to do that:

public BitmapDrawable imageOnDrawable(int drawableBackground, Bitmap customImage)
{
//The following line is optional but I'd advise you to minimize the size of 
//the size of the bitmap (using a thumbnail) in order to improve draw
//performance of the overlays (especially if you are creating a lot of overlays).

Bitmap customImageThumbnail = ThumbnailUtils.extractThumbnail(
                                                customImage, 100, 100); 

Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);
bm = Bitmap.createScaledBitmap(bm, 112, 120, false);

Canvas canvas = new Canvas(bm);
canvas.drawBitmap(bm, 0, 0, null);
// The 6,6 in the below line refer to the offset of the customImage/Thumbnail
// from the top-left corner of the background box (or whatever you want to use
// as your background) 
canvas.drawBitmap(customImageThumbnail, 6, 6, null); 

return new BitmapDrawable(bm);

}
A Random Android Dev
  • 2,691
  • 1
  • 18
  • 17
  • I have OverlayItems i'm drawing what i don't know is if this canvas draws the overlay item within the visible rect of the map view or not… because if not i won't draw the overlayitem to improve the speed of the map view – lukaswelte Aug 22 '12 at 16:01
  • That seems tough to determine simply based on the canvas because the canvas has no geographical bearing of it's own whereas overlays contained in the itemized overlays have their own GeoPoints. So I was wondering if you are using the Itemized Overlay as a container for multiple overlays or are you using different itemized overlays objects for each of your overlays? – A Random Android Dev Aug 22 '12 at 16:32
  • An Itemized Overlay contains multiple OverlayItems but there are more than one Itemized Overlay… So is it better/easier doing this in the Draw of the OverlayItem? If yes how can i get the drawed rect there? – lukaswelte Aug 22 '12 at 16:39
0

I now did this by iterating over the items and checking wether they are on the map or not Just like this:

@Override
    public void draw(Canvas canvas, MapView map, boolean shadow) {
        for (int i = 0; i < mOverlays.size(); i++) {
            if (isLocationVisible(mOverlays.get(i).getPoint())) {
                super.draw(canvas, map, false);
            }
        }       
    } 
lukaswelte
  • 2,951
  • 1
  • 23
  • 45