0

I am building a Google maps app. I have different objects with coordinates but each object has a unique int value which I would like to be shown next to a marker. For example for an object with specific coordinates and value 123, I would like to put on the map(at those coords) the marker and next to it the value 123.

I've been doing some research and the only way I found plausible is to use an Android API to create your own bitmap image from a base image and some string that is "attached" and use that for the marker icon.

Is there a better way of doing that?
On the same topic, can you show the title of every marker on your map at the same time?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
knightLoki
  • 480
  • 1
  • 6
  • 11

2 Answers2

1

https://stackoverflow.com/a/14812104

Please see the link. The snipet is used to add text on the maker which can also be customized.

1

Yes @kisslory you can fully customize each marker to suit your need.

While setting the bitmap for each marker you can create a new bitmap with from a given resource using the method below.

public static Bitmap drawTextToBitmap(Context gContext,
                               int gResId,
                               String gText) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap =
            BitmapFactory.decodeResource(resources, gResId);

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if(bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (14 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width())/2;
    int y = (bitmap.getHeight() + bounds.height())/2;

    canvas.drawText(gText, x, y, paint);

    return bitmap;
}
Sunday G Akinsete
  • 802
  • 13
  • 14