有没有办法在 Android Studio 中自定义每个 Google 地图标记?

问题描述 投票:0回答:2

我正在构建一个谷歌地图应用程序。我有不同的带有坐标的对象,但每个对象都有一个唯一的 int 值,我希望将其显示在标记旁边。 例如,对于具有特定坐标和值 123 的对象,我想在地图上(在这些坐标处)放置标记,并在其旁边放置值 123。

我一直在做一些研究,我发现唯一可行的方法是使用 Android API 从基本图像和一些“附加”的字符串创建您自己的位图图像,并将其用作标记图标。

有更好的方法吗?
对于同一主题,您可以同时显示地图上每个标记的标题吗?

android google-maps google-maps-markers
2个回答
1
投票

https://stackoverflow.com/a/14812104

请查看链接。片段用于在制作器上添加文本,也可以自定义。


1
投票

是的@kisslory,您可以完全自定义每个标记以满足您的需要。

在为每个标记设置位图时,您可以使用以下方法从给定资源创建新位图。

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.