Here is a code which is actually working, but with manual draw, you
can imitate my workaround for now.
I hope an official answer will come up fast...
/** Map layer displaying contact locations */
public class LocationOverlay extends
ItemizedOverlay<LocationOverlayItem>
{
/** Contacts with known location */
private final List<Contact> mContacts;
/*
* FIXME fields to manually draw items, we don't know how to make
* ItemizedOverlay actually work
*/
private Bitmap mBubbleBitmap;
private Paint mInnerPaint;
private Paint mBorderPaint;
private TextPaint mTextPaint;
private static final int TEXT_OFFSET_X = 10;
private static final int TEXT_OFFSET_Y = 15;
private static final int INFO_WINDOW_HEIGHT = 25;
/** Create a new instance */
public LocationOverlay()
{
super(UbikIMApplication.getApplicationResources().getDrawable(
R.drawable.bubble));
/* Get contacts with location */
mContacts = new ArrayList<Contact>();
for (Contact contact : ContactManager.getInstance().getContacts())
if (contact.getLocation() != null)
mContacts.add(contact);
/* Populate */
populate();
/* FIXME manual draw managing relating code */
/* Decompress bubble bitmap */
mBubbleBitmap = BitmapFactory.decodeResource(UbikIMApplication
.getApplicationResources(), R.drawable.bubble);
/* Init painter used to draw the inner of the info window */
mInnerPaint = new Paint();
mInnerPaint.setARGB(225, 75, 75, 75);
mInnerPaint.setAntiAlias(true);
/* Init painter used to draw the border of the info window */
mBorderPaint = new Paint();
mBorderPaint.setARGB(255, 255, 255, 255);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setStyle(Style.STROKE);
mBorderPaint.setStrokeWidth(2);
/* Init painter used to draw the text inside the info window */
mTextPaint = new TextPaint();
mTextPaint.setARGB(255, 255, 255, 255);
mTextPaint.setAntiAlias(true);
}
@Override
protected LocationOverlayItem createItem(int i)
{
return new LocationOverlayItem(mContacts.get(i));
}
@Override
public int size()
{
return mContacts.size();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
/* FIXME manual draw because automatic doesn't seem to work */
/* Retrieve map view projection to compute coordinates */
Projection projection = mapView.getProjection();
/* For all contacts with location (including self) */
for (int index = size() - 1; index >= 0; index--)
{
OverlayItem item = getItem(index);
String title = item.getTitle();
Point point = projection.toPixels(item.getPoint(), null);
/* Draw bubble */
canvas.drawBitmap(mBubbleBitmap, point.x -
mBubbleBitmap.getWidth() / 2,
point.y - mBubbleBitmap.getHeight(), null);
/* Compute info window geometry */
int INFO_WINDOW_WIDTH = getTextWidth(title) + TEXT_OFFSET_X * 2;
RectF infoWindowRect = new RectF(0, 0, INFO_WINDOW_WIDTH,
INFO_WINDOW_HEIGHT);
int infoWindowOffsetX = point.x - INFO_WINDOW_WIDTH / 2;
int infoWindowOffsetY = point.y - INFO_WINDOW_HEIGHT
- mBubbleBitmap.getHeight() - 2;
infoWindowRect.offset(infoWindowOffsetX, infoWindowOffsetY);
/* Draw inner info window */
canvas.drawRoundRect(infoWindowRect, 5, 5, mInnerPaint);
/* Draw border for info window */
canvas.drawRoundRect(infoWindowRect, 5, 5, mBorderPaint);
/* Draw user name in the info window */
canvas.drawText(title, infoWindowOffsetX + TEXT_OFFSET_X,
infoWindowOffsetY + TEXT_OFFSET_Y, mTextPaint);
}
super.draw(canvas, mapView, shadow);
}
/* FIXME manual draw related function */
/** @return text width in pixels that the text paint will use to
draw text */
private int getTextWidth(String text)
{
int count = text.length();
float[] widths = new float[count];
mTextPaint.getTextWidths(text, widths);
int textWidth = 0;
for (int i = 0; i < count; i++)
textWidth += widths[i];
return textWidth;
}
@Override
protected boolean onTap(int index)
{
setFocus(getItem(index));
return true;
}
@Override
protected boolean hitTest(LocationOverlayItem item, Drawable marker,
int hitX, int hitY)
{
return super.hitTest(item, marker, hitX, hitY +
mBubbleBitmap.getHeight()
/ 2);
}
On 27 août, 20:49, "Chris Chiappone" <[EMAIL PROTECTED]> wrote:
> Same problem here, i converted code normal overlay code that worked in
> order to use the "recommended" ItemizedOverlay. Can anyone confirm
> that this is a bug or are we just doing something wrong.
>
> Thanks.
>
> On Tue, Aug 26, 2008 at 9:52 AM, Guillaume Perrot
>
>
>
> <[EMAIL PROTECTED]> wrote:
>
> > Hi,
> > I have the same problem.
> > The draw function is called, I tried to override it, I even added a
> > bitmap in the top left corner for test purposes, I see my dummy bitmap
> > but no items...
>
> > On 26 août, 07:50, marcel-182 <[EMAIL PROTECTED]> wrote:
> >> Hi,
>
> >> Same problem here. I added two items and I found out that even the
> >> draw() method from ItemizedOverlay is called but there is nothing on
> >> my map view. My code is quite equal to yours.
>
> >> On 25 Aug., 18:29, Reto <[EMAIL PROTECTED]> wrote:
>
> >> > I've been trying to use the ItemizedOverlay and OverlayItem classes in
> >> > the 0.9 Beta to simulate map markers but have been having some
> >> > problems getting it to display on the map.
>
> >> > Once I've implemented my own ItemizedOverlay (and overriden
> >> > createItem), creating a new instance of my class seems to work (I can
> >> > extract OverlayItems from it) but adding it to the overlay list for
> >> > one of my maps doesn't seem to do anything.
>
> >> > I've included the code for adding the ItemizedOverlay to the map, and
> >> > the ItemizedOverlay implementation itself is also included. Have I
> >> > done something wrong or is this functionality not yet available?
>
> >> > // Add the ItemizedOverlay to the Map
> >> > private void addItemizedOverlay() {
> >> > Resources r = getResources();
> >> > mapView = (MapView)findViewById(R.id.map_view);
> >> > List<Overlay> overlays = mapView.getOverlays();
>
> >> > MyItemizedOverlay markers = new
> >> > MyItemizedOverlay(r.getDrawable(R.drawable.icon));
> >> > overlays.add(markers);
>
> >> > OverlayItem oi = markers.getItem(0);
> >> > markers.setFocus(oi);
> >> > mapView.postInvalidate();
>
> >> > }
>
> >> > Where MyItemizedOverlay is defined as:
>
> >> > public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
> >> > public MyItemizedOverlay(Drawable defaultMarker) {
> >> > super(defaultMarker);
> >> > populate();
> >> > }
>
> >> > @Override
> >> > protected OverlayItem createItem(int index) {
> >> > Double lat = (index+37.422006)*1E6;
> >> > Double lng = -122.084095*1E6;
> >> > GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
>
> >> > OverlayItem oi = new OverlayItem(point, "Marker", "Marker Text");
> >> > return oi;
> >> > }
>
> >> > @Override
> >> > public int size() {
> >> > return 5;
> >> > }
>
> >> > }
>
> --
> ~chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new Android 0.9 SDK beta!
http://android-developers.blogspot.com/2008/08/announcing-beta-release-of-android-sdk.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---