Draw Text on Google Map no longer possible?

假装没事ソ 提交于 2019-12-03 15:56:41

You can create a Marker from a View by using the following code:

public static Bitmap createDrawableFromView(Context context, View view) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}

Source: http://www.nasc.fr/android/android-using-layout-as-custom-marker-on-google-map-api/

edit: If you use more than a single marker, make sure you don't do the DisplayMetrics and view setup stuff (everything above Bitmap bitmap =.....) for every marker. That would slow down your app dramatically.

jgonza73

I had the same problem, trying to upgrade from v1 to v2. Finally I used a marker, created a Bitmap with the text, and used it as icon of the marker.

First of all, you have to create de Bitmap with the text. Note: configure paintText with text properties (color, typeface, textalign,...)

Rect boundsText = new Rect();
paintText.getTextBounds(strText, 0, strText.length(), boundsText);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmpText = Bitmap.createBitmap(boundsText.width(), boundsText.height(), conf);

Then, use Canvas for drawing the text. It is a little bit madness fix text with canvas dimenssion.

Canvas canvasText = new Canvas(bmpText);
canvasText.drawText(strText, canvasText.getWidth() / 2, canvasText.getHeight(), paintText);

Finally use Bitmap to create icon in MarkerOption

MarkerOptions markerOptions = new MarkerOptions()
    .position(latlngMarker)
    .icon(BitmapDescriptorFactory.fromBitmap(bmpText))
    .anchor(0.5f, 1);

Hope it could help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!