2017-06-14 189 views
0

我正在构建谷歌地图应用程序。我有不同的坐标对象,但每个对象都有一个唯一的int值,我希望显示在标记旁边。 例如,对于具有特定坐标和值123的物体,我想在地图上(在这些坐标上)标记并在其旁边显示值123.
我一直在做一些研究,发现合理的是使用Android API从基本图像创建自己的位图图像和一些“附加”并用于标记图标的字符串。
有没有更好的方法来做到这一点?
在同一主题上,您可以同时显示地图上每个标记的标题吗?有没有在Android Studio中自定义每个Google Maps标记的方法?

回答

1

https://stackoverflow.com/a/14812104

请参阅链接。 Snipet用于在制造商上添加文字,也可以定制。

+1

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

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; 
} 
相关问题