Can I draw text on a Google Map anymore?

I am updating the Android application from version 1 to version 2 of the Google Android APIs. In my version 1 code, I was able to draw text directly on the map in my subclass of ItemizedOverlay, overriding the draw () method as follows. The text I want to draw is a dynamic, additional text element that I want to display next to each map marker, so the text will be added / removed often when different characters are displayed / deleted.

@Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { if (!shadow) { canvas.drawText("some text", (float) point.x + TextOffsetX , (float) point.y + TextOffsetY, m_paint); } return super.draw(canvas, mapView, shadow, when); } 

However, this is not possible in version 2 of the API. This is actually not the concept of ItemizedOverlays, and nothing can be subclassed. Is there a way to draw text in GoogleMap in the new version of the API?

+6
source share
2 answers

You can create a marker from a view 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 one marker, make sure that you do not do DisplayMetrics and do not view the configuration files (all higher Bitmap bitmap = .....) for each marker. This will significantly reduce your application.

+4
source

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

First of all, you need to create a bitmap with text. Note: configure paintText with text properties (color, font, text, ...)

 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 to draw text. This is a little fix text madness with canvas reduction.

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

Finally, use Bitmap to create an icon in MarkerOption

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

Hope this helps you.

+3
source

All Articles