Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Osmdroid insert text inside marker

How I can insert text inside Osmdroid marker? Suppose i need place several markers on map, and each marker should have number inside it. enter image description here How I can make it? I try #setTitle() method, but it place text in balloon.

Update:

for(int i = 0; i < orders.size(); i++) {
    mOrderPoint = orders.get(i).getStart();
    final Order order = orders.get(i);
    Marker orderMarker = new Marker(mMap);
    orderMarker.setIcon(ContextCompat.getDrawable(mContext,R.drawable.order_pin));
    orderMarker.setPosition(mOrderPoint);
}
like image 436
igor_rb Avatar asked Aug 19 '15 07:08

igor_rb


2 Answers

This method takes a drawable from your resources, draws some text on top of it and returns the new drawable. All you need to do is give it the resource id of your bubble, and the text you want on top. Then you can pass the returned drawable wherever you want it.

public BitmapDrawable writeOnDrawable(int drawableId, String text){

        Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

        Paint paint = new Paint(); 
        paint.setStyle(Style.FILL);  
        paint.setColor(Color.BLACK); 
        paint.setTextSize(20); 

        Canvas canvas = new Canvas(bm);
        canvas.drawText(text, 0, bm.getHeight()/2, paint);

        return new BitmapDrawable(bm);
    }

Note:

To preserve density you need this constructor

BitmapDrawable (Resources res, Bitmap bitmap)

So, keeping your context, last return should be something like

    return new BitmapDrawable(context.getResources(), bm);

This prevent an undesired resized drawable.

like image 183
Blue_Alien Avatar answered Sep 19 '22 20:09

Blue_Alien


You want to look at osmdroid bonus pack.

Website: https://github.com/MKergall/osmbonuspack

There are many examples of stylized bubbles with text in it.

like image 40
spy Avatar answered Sep 22 '22 20:09

spy