doovers doovers - 3 months ago 41
Android Question

MapBox custom marker not working

I'm trying to set a dynamic custom marker on a map but it is not working and instead showing the default marker. Where am I going wrong here?

IconFactory iconFactory = IconFactory.getInstance(MainActivity.this);
TextDrawable iconDrawable = new TextDrawable("CD");
Icon icon = iconFactory.fromDrawable(iconDrawable);

MarkerOptions marker = new MarkerOptions()
.position(new LatLng(location.latitude, location.longitude))
.icon(icon)

mapboxMap.addMarker(marker);


TextDrawable:

public class TextDrawable extends Drawable {

private final String text;
private final Paint paint;

public TextDrawable(String text) {

this.text = text;

this.paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(22f);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.FILL);
paint.setTextAlign(Paint.Align.LEFT);
}

@Override
public void draw(Canvas canvas) {
canvas.drawText(text, 0, 0, paint);
}

@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}

@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}

Answer

use this code :

Marker myLocMarker = map.addMarker(new MarkerOptions()
            .position(myLocation)
            .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here"))));

Blue box is just a rectangle resource that we will use as a background

private Bitmap writeTextOnDrawable(int drawableId, String text) {

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

    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTypeface(tf);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(convertToPixels(context, 11));

    Rect textRect = new Rect();
    paint.getTextBounds(text, 0, text.length(), textRect);

    Canvas canvas = new Canvas(bm);

    //If the text is bigger than the canvas , reduce the font size
    if(textRect.width() >= (canvas.getWidth() - 4))     //the padding on either sides is considered as 4, so as to appropriately fit in the text
        paint.setTextSize(convertToPixels(context, 7));        //Scaling needs to be used for different dpi's

    //Calculate the positions
    int xPos = (canvas.getWidth() / 2) - 2;     //-2 is for regulating the x position offset

    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;  

    canvas.drawText(text, xPos, yPos, paint);

    return  bm;
}



public static int convertToPixels(Context context, int nDP)
{
    final float conversionScale = context.getResources().getDisplayMetrics().density;

    return (int) ((nDP * conversionScale) + 0.5f) ;

}
Comments