【问题标题】:How to label polygons in Android Google Map API v2如何在 Android Google Map API v2 中标记多边形
【发布时间】:2015-03-16 19:52:06
【问题描述】:

我似乎不知道如何在 Android 应用程序的 Google 地图上打印我自己的文本。这应该是可能的,对吧?但在 API 文档中,我发现的只是放置标记(不是我想要的)或图像(也不是我想要的)的能力。

【问题讨论】:

标签: java android google-maps android-maps-v2


【解决方案1】:

我使用类似这样的东西来添加带有文本的叠加层 将其转换为位图,然后将其添加为叠加层。 在 textAsBitmap() 方法中,您将看到我正在使用资源作为文本的背景。如果您只想要文本,请使用透明 png

LatLng pos = new LatLng(-48.59, -2.30);

GroundOverlayOptions iroad = new GroundOverlayOptions();

iroad.image(BitmapDescriptorFactory.fromBitmap(textAsBitmap("Ineseno Road", 100, 0xffffffff)));

.positionFromBounds(ir).zIndex(3);
iroad.position(pos, 8600f);
iroad.zIndex(4);
iroad.anchor(a, b);
iroad.bearing(0);
iroad.visible(true);
iroad.transparency(0);

mMap.addGroundOverlay(iroad);

要使用它,你需要这个方法:

public Bitmap textAsBitmap(String text, float textSize, int textColor) {

    Paint paint = new Paint();
    paint.setTextSize(textSize);
    paint.setColor(textColor);
    paint.setStrokeWidth(5);
    paint.setShadowLayer(5, 0, 0, 0xff000000);
    paint.setTextAlign(Paint.Align.LEFT);

    int width = (int) (paint.measureText(text) + 0.5f); // round
    float baseline = (int) (-paint.ascent() + 0.5f); // ascent() is negative
    int height = (int) (baseline + paint.descent() + 0.5f);

    Bitmap image = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(image);
    canvas.drawText(text, 0, baseline, paint);

    Drawable drawable2 = new BitmapDrawable(getResources(), image);
    Drawable drawable1 = getResources().getDrawable(R.drawable.black_box);

    Drawable[] layers = new Drawable[2];
    layers[0] = drawable1;
    layers[1] = drawable2;

    LayerDrawable layerDrawable = new LayerDrawable(layers);
    layerDrawable.setLayerInset(1, 0, 0, 0, 0);

    Bitmap bitmap = Bitmap.createBitmap((width + 5), (height + 5),
            Bitmap.Config.ARGB_8888);

    Canvas can = new Canvas(bitmap);
    layerDrawable.setBounds(0, 0, width, height);
    layerDrawable.draw(can);

    BitmapDrawable bitmapDrawable = new BitmapDrawable(this.getResources(),
            bitmap);
    bitmapDrawable.setBounds(0, 0, (width + 5), (height + 5));

    return bitmapDrawable.getBitmap().copy(
            Config.ARGB_8888, true);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多