【问题标题】:Is there a way of customizing each Google Maps marker in Android Studio?有没有办法在 Android Studio 中自定义每个谷歌地图标记?
【发布时间】:2017-06-16 00:42:35
【问题描述】:

我正在构建一个谷歌地图应用程序。我有不同的坐标对象,但每个对象都有一个唯一的 int 值,我想显示在标记旁边。 例如,对于具有特定坐标和值 123 的对象,我想在地图上(在那些坐标处)放置标记,并在其旁边放置值 123。
我一直在做一些研究,我发现唯一可行的方法是使用 Android API 从基本图像和一些“附加”的字符串创建自己的位图图像,并将其用于标记图标。
有没有更好的方法呢?
在同一主题上,您能否同时显示地图上每个标记的标题?

【问题讨论】:

    标签: android google-maps google-maps-markers


    【解决方案1】:

    https://stackoverflow.com/a/14812104

    请查看链接。 snipet 用于在 maker 上添加文本,也可以自定义。

    【讨论】:

    • 是的@kisslory,您可以完全自定义每个标记以满足您的需要。
    【解决方案2】:

    是的@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;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 2018-01-13
      • 2020-10-02
      • 1970-01-01
      • 2016-01-16
      • 2011-04-17
      相关资源
      最近更新 更多