0

I have a MapView that works properly and is setup, the overlay items being drawn (when simply referenced as a drawable) work just fine. However, each drawable item needs to include a numerical value on it (e.g, overlay item 1, needs to display a #1 on it). I created the layout style R.layout.map_bubble_standard. I'm trying to convert that view into a bitmap, and that bitmap into a drawable to be displayed on the map. However, when the map loads, nothing shows up. Here's what I'm doing...

        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(     Context.LAYOUT_INFLATER_SERVICE );
        LinearLayout bubbleView = (LinearLayout)inflater.inflate(R.layout.map_bubble_standard, null);

        bubbleView.setDrawingCacheEnabled(true);
        bubbleView.buildDrawingCache();

        //drawable = _context.getResources().getDrawable(R.drawable.map_pin_48);
        drawable = new BitmapDrawable(_context.getResources(), bubbleView.getDrawingCache());
        bubbleView.setDrawingCacheEnabled(false);

    OverlayItem oi = _overlays.get(whichOne); 
    oi.setMarker(boundCenterBottom(drawable));

My code completes the method just fine, but nothing is drawn on the map. Can anyone help determine what I'm doing incorrectly?

[Edit] - Resolved

I used a another link that I found, that offered advice for fixing this: Here

Hopefully this can help someone else out in the future as well. For reference, I also moved the inflater for the view and the inflater object itself into the default constructor for my itemized overlay class.

_bubbleLayout.setDrawingCacheEnabled(true);

        _bubbleLayout.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        _bubbleLayout.layout(0, 0, _bubbleLayout.getMeasuredWidth(), _bubbleLayout.getMeasuredHeight()); 

        _bubbleLayout.buildDrawingCache(true);
        Bitmap b = Bitmap.createBitmap(_bubbleLayout.getDrawingCache());
        _bubbleLayout.setDrawingCacheEnabled(false); // clear drawing cache 

        //drawable = _context.getResources().getDrawable(R.drawable.map_pin_48);
        drawable = (Drawable) new BitmapDrawable(_context.getResources(),b);



    OverlayItem oi = _overlays.get(whichOne); 
    oi.setMarker(boundCenterBottom(drawable));
Community
  • 1
  • 1
RyanInBinary
  • 1,533
  • 3
  • 19
  • 47

1 Answers1

1

Not sure if this is right, but according to the documentation:

To benefit from the cache, you must request the drawing cache by calling getDrawingCache() and draw it on screen if the returned bitmap is not null.

To me that means you must have drawn the View once on the screen before you get anything back. If its never been actually drawn then it has no cache to pull from. Are you making this visible on the screen first?

Salil Pandit
  • 1,498
  • 10
  • 13
  • Currently no, can I just add it to the mapview layout and hide it below the screen and then use the drawing cache from there? – RyanInBinary Jul 25 '12 at 18:07
  • @RyanInBinary Well why not just add the view itself to the MapView in the coordinates that you want? Whats the need to make it a bitmap? Are you looking to make bubbles? If so I would recommend ditching the bitmap idea and going with: https://github.com/jgilfelt/android-mapviewballoons/ its very easy to use and lots of examples/SO posts answering questions about it... – Salil Pandit Jul 25 '12 at 18:11
  • I have overlay bubbles already, I'm looking at having the actual map-pin objects that are being drawn against the map (before tapped) to be slightly more dynamic than they are... created from an XML layout. This library does a good job of creating the pop-up bubble in a custom way (which I'm already doing)... I need the initial pin to be a bit more specialized though. – RyanInBinary Jul 25 '12 at 18:57
  • Hmm thats fair. I'm guessing these are too complex to just have three or four drawables in your folder (ie. more than just a blue, red, and green marker). Well since MapView extends ViewGroup, my guess is you can just skip the drawables and use the MapView#addView() function and use MapView.LayoutParams which takes a GeoPoint to place the markers on the map. Might be a bit of work to maintain them though. https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/MapView.LayoutParams – Salil Pandit Jul 25 '12 at 19:59
  • Also looks like you can just generate it off screen: http://stackoverflow.com/a/4858154/960048 – Salil Pandit Jul 25 '12 at 22:07