如何创建自定义android地图标记?

时间:2010-09-24 13:05:51

标签: android android-mapview drawable google-maps-markers

我想创建一个包含4个元素的mapview标记: - 图片(用户) - 背景 - 文字(用户名) - 箭头显示方向

OverlayItem.setMarker()只接受一个drawable。

如何创建包含4个项目的drawable?或者我可以将一个视图作为标记添加到overlayItem?

任何想法?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。 我创建了一个BitmapDrawable

这是代码

        Bitmap photo = ((BitmapDrawable)mActivity.getResources().getDrawable(R.drawable.user_picture_dummy)).getBitmap();

        Bitmap returnedBitmap = Bitmap.createBitmap(Global.dipToPx(40), Global.dipToPx(67), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);

        mBackground.setBounds(0, Global.dipToPx(10), canvas.getWidth(), canvas.getHeight());
        mBackground.draw(canvas);

        m.postScale((float)Global.dipToPx(30)/(float)photo.getWidth(), (float)Global.dipToPx(30)/(float)photo.getHeight());
        m.postTranslate(Global.dipToPx(5), Global.dipToPx(15));
        canvas.drawBitmap(photo, m, paint);
        m.reset();

        canvas.drawText(Global.DateToTimeString(adv.getStartDateMS()), Global.dipToPx(20), Global.dipToPx(10), paint);

        mat.postRotate((float)bearingTo(adv.getDepGeoPoint(), adv.getArrLocationObj().getGeoPoint()), mDirection.getWidth()/2, mDirection.getHeight()/2);
        mat.postTranslate(canvas.getWidth()-mDirection.getWidth(), canvas.getHeight()-mDirection.getHeight()-Global.dipToPx(12));
        canvas.drawBitmap(mDirection, mat, paint);
        mat.reset();

        BitmapDrawable drawable = new BitmapDrawable(returnedBitmap);
        boundCenter(drawable);

        OverlayItem item = new OverlayItem(adv.getDepGeoPoint(), adv.getOwnerObj().getUserName(), adv.getOwnerObj().getUserName());
        item.setMarker(drawable);