Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps API for android (v2) - how to add text with no background?

I need to make a text annotation for a specific location (lat,lng) on google map (just text with no background, as street names in google map hybrid satellite view). The text should move accordingly when user rotates/moves the map. Is this possible?

like image 899
Deinlandel Avatar asked Jan 31 '26 08:01

Deinlandel


1 Answers

You can create you own Marker-Icon dynamically and draw into it whatever you want, of course also text only.

public BitmapDescriptor createPureTextIcon(String text) {

    Paint textPaint = new Paint(); // Adapt to your needs

    float textWidth = textPaint.measureText(text);
    float textHeight = textPaint.getTextSize();
    int width = (int) (textWidth);
    int height = (int) (textHeight);

    Bitmap image = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(image);

    canvas.translate(0, height);

    // For development only:
    // Set a background in order to see the
    // full size and positioning of the bitmap.
    // Remove that for a fully transparent icon.
    canvas.drawColor(Color.LTGRAY);

    canvas.drawText(text, 0, 0, textPaint);
    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(image);
    return icon;
}
like image 190
user2808624 Avatar answered Feb 01 '26 21:02

user2808624



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!