如何在Android Google Map API v2中标记多边形

时间:2015-03-16 19:52:06

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

我似乎无法弄清楚如何在Android应用中的Google地图上打印我自己的文字。这应该是可能的,对吗?但是在API文档中,我发现的是能够放置标记(不是我想要的)或图像(也不是我想要的)。

1 个答案:

答案 0 :(得分:0)

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

,请使用透明的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);
}