Android百度地图SDK文字覆盖物的换行和旋转中心点问题

百度地图的Android SDK中支持在地图上添加各种覆盖物,其中包括文字覆盖物TextOptions,但目前版本4.1.0中此功能不支持文字换行,且不可设置文字中心点,导致如果想要和其它覆盖物混合使用时,如在图标上显示一些文字时,效果很不好,由于只能通过对文字覆盖物的经纬度坐标添加一定的偏移来实现,例如下面这样:

                        // 定位坐标偏移
                        point = new LatLng(lat+0.0001, lng);
                        OverlayOptions textOption = new TextOptions()
                                .bgColor(0xAAFFFF00)
                                .fontSize(24)
                                .fontColor(0xFFFF00FF)
                                .text("test "+formatter.format(date))
                                .position(point);
                        // 在地图上添加该文字对象并显示
                        mBaiduMap.addOverlay(textOption);



可以看到由于中心点偏差导致的效果很不理想,为了解决这个问题,可以采用TextView渲染Bitmap然后添加为图标覆盖物的方式,这样既可以实现换行,也可以控制中心点,实现正常的地图视口旋转等效果,显示如下:

                        TextView textView = new TextView(MapFragment.this.getActivity());
                        textView.setGravity(Gravity.CENTER);
//                        textView.setBackgroundResource(R.drawable.icon_gcoding);
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
                        textView.setTextColor(Color.RED);
                        textView.setShadowLayer(3, 0, 0, Color.BLACK);
                        textView.setText("test\n"+formatter.format(date)+"\n");
                        textView.destroyDrawingCache();
                        textView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                        textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
                        textView.setDrawingCacheEnabled(true);
                        Bitmap bitmapText = textView.getDrawingCache(true);
                        BitmapDescriptor bd = BitmapDescriptorFactory.fromBitmap(bitmapText);
                        OverlayOptions oo = new MarkerOptions().icon(bd).
                                position(point);
                        mBaiduMap.addOverlay(oo);
                        bd.recycle();

博主友情提示:

如您在评论中需要提及如QQ号、电子邮件地址或其他隐私敏感信息,欢迎使用>>博主专用加密工具v3<<处理后发布,原文只有博主可以看到。