[英]Google Maps Text Overlay - Android
I am trying to colour some building on Google Maps like this: 我试图在Google地图上为某些建筑物上色,如下所示:
I am able to overlay a polygon object on top of the building but Im not able to apply a label on top of it (Building's ID). 我能够在建筑物顶部覆盖多边形对象,但我无法在其上面应用标签(建筑物的ID)。
How can overlay text on buildings like it's shown in the image above with Android? 如何使用Android在上图中显示的建筑物上覆盖文字?
I know this question is a bit old, but I just figured out how to do this for my own project and thought I'd share. 我知道这个问题有点陈旧,但我只是想出了如何为我自己的项目做这个,并认为我会分享。 The method is to create a bitmap image of your text, and then display it as a GroundOverlay on your polygon with a zIndex greater than the polygon.
方法是创建文本的位图图像,然后在多边形上将其显示为GroundOverlay,其zIndex大于多边形。
If you've got the list of LatLngs for your polygon you can do the following: 如果您已获得多边形的LatLng列表,则可以执行以下操作:
private void showText(List<LatLng> latLngList) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng latLng : latLngList) {
builder.include(latLng);
}
googleMap.addGroundOverlay(new GroundOverlayOptions()
.positionFromBounds(builder.build())
.image(
BitmapDescriptorFactory.fromBitmap(
getBitmapFromView()
)
)
.zIndex(100)
);
}
The .zIndex(100) part is important since you'll want the text to be displayed over your polygon. .zIndex(100)部分很重要,因为您希望文本显示在多边形上。 (I'm not actually sure what the maximum z index is but 100 works).
(我实际上不确定最大z索引是多少,但100个工作)。
Here's the method to transform a layout into a bitmap. 这是将布局转换为位图的方法。
private Bitmap getBitmapFromView() {
View customView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.my_text_layout, null);
customView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
customView.layout(0, 0, customView.getMeasuredWidth(), customView.getMeasuredHeight());
customView.buildDrawingCache();
Bitmap returnedBitmap = Bitmap.createBitmap(customView.getMeasuredWidth(), customView.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = customView.getBackground();
if (drawable != null) {
drawable.draw(canvas);
}
customView.draw(canvas);
return returnedBitmap;
}
If you just want the text, then your layout 'R.layout.my_text_layout' can just be a layout with a TextView. 如果您只想要文本,那么您的布局'R.layout.my_text_layout'可以只是一个带TextView的布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.