Google Maps文字叠加层-Android [英] Google Maps Text Overlay - Android

查看:52
本文介绍了Google Maps文字叠加层-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Google Maps上的某些建筑物着色:

I am trying to colour some building on Google Maps like this:

我可以在建筑物顶部覆盖多边形对象,但是我不能在建筑物顶部添加标签(建筑物ID).

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).

如何使用Android将图片覆盖上图所示的建筑物?

How can overlay text on buildings like it's shown in the image above with Android?

推荐答案

我知道这个问题有点老了,但是我只是想出了如何为自己的项目做到这一点,并认为可以分享.该方法是创建文本的位图图像,然后在zIndex大于多边形的多边形上将其显示为GroundOverlay.

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.

如果您有多边形的LatLng列表,则可以执行以下操作:

If you've got the list of LatLngs for your polygon you can do the following:

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)
    );
}

.zIndex(100)部分非常重要,因为您希望将文本显示在多边形上.(我实际上不确定最大z索引是多少,但100可以工作.)

The .zIndex(100) part is important since you'll want the text to be displayed over your polygon. (I'm not actually sure what the maximum z index is but 100 works).

这是将布局转换为位图的方法.

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;
}

如果只需要文本,则布局"R.layout.my_text_layout"可以只是具有TextView的布局.

If you just want the text, then your layout 'R.layout.my_text_layout' can just be a layout with a TextView.

这篇关于Google Maps文字叠加层-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆