4

Here is a screenshot of something which I'm trying to achieve

enter image description here

I would like to add unique label (like numbers) to each overlayitem of the map. I have done the basic part of adding the overlayitems and showing them on the map. But this where I am stuck for a long time

pankajagarwal
  • 13,462
  • 14
  • 54
  • 65

1 Answers1

6

You can add OverlayItem with different markers on the same ItemizedOverlay by using function:

overlayItem.setMarker(drawable);

For this to work, you need to set the bounds on the Drawable:

Drawable icon1 = getResources().getDrawable(R.drawable.icon1);
Drawable icon2 = getResources().getDrawable(R.drawable.icon2);
icon1.setBounds(0, 0, icon1.getIntrinsicWidth(), icon1.getIntrinsicHeight());
icon2.setBounds(0, 0, icon2.getIntrinsicWidth(), icon2.getIntrinsicHeight());
OverlayItem item1 = new OverlayItem(new Point(48858290, 2294450), 
    "Tour Eiffel", "La tour Eiffel");
OverlayItem item2 = new OverlayItem(new Point(48873830, 2294800), 
    "Arc de Triomphe", "L'arc de triomphe");                        
item1.setMarker(icon1);
item2.setMarker(icon2);

You will need as much Bitmaps as your max number of markers. But it will be faster than dynamically draw text on bitmaps. It's a mobile phone, processors are not fast. If you prefer to draw text on a Bitmap anyway, Ii's really easy, you can do it like this:

//get a reference on the ImageView 
ImageView iv = (ImageView)findViewById(R.id.myImage);

// load the marker image
Bitmap myRefBitmap = BitmapFactory.decodeResource(getResources(), 
    R.drawable.icon);

// create a mutable bitmap with the same size as the marker image
Bitmap myWrittenBitmap = Bitmap.createBitmap(myRefBitmap.getWidth(), 
    myRefBitmap.getHeight(), Bitmap.Config.ARGB_4444);

// create a Canvas on which to draw and a Paint to write text.
Canvas canvas = new Canvas(myWrittenBitmap);
Paint txtPaint = new Paint();
txtPaint.setColor(Color.RED);
txtPaint.setTextSize(12);
txtPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
txtPaint.setTypeface(Typeface.DEFAULT_BOLD);

//draw ref bitmap then text on our canvas
canvas.drawBitmap(myRefBitmap, 0, 0, null);
canvas.drawText("Droid", 5, 15, txtPaint);

// set the new written bitmap into the ImageView
iv.setImageBitmap(myWrittenBitmap);
pcans
  • 7,611
  • 3
  • 32
  • 27
  • but I just want to add labels on the marker. And I can have more than 100 markers. Adding so many markers as resource doesn't seem to be a good idea – pankajagarwal Mar 23 '11 at 04:48
  • I edited the answer and added the way to draw text on bitmap. But i really think it's better on a mobile plateform to have precompiled ressources than dynamically generated ones. – pcans Mar 23 '11 at 09:07
  • Thanks for your reply, but as you suggested creating a bitmap from a drawable and vice-versa is really costly. Instead I am thinking to override the onDraw method of my ItemizedOverlay class, iterate through all its items and then draw the text accordingly. – pankajagarwal Mar 23 '11 at 09:26
  • Once the bitmaps are generated, the default onDraw behavior will use caching, if you really want to draw the text yourself, it's best to do it in the onCreate method. – pcans Mar 23 '11 at 10:26