为了账号安全,请及时绑定邮箱和手机立即绑定

具有自定义标记的android Maps API v2

具有自定义标记的android Maps API v2

吃鸡游戏 2019-12-25 11:14:11
我想用自定义标记制作地图。在API v2中,我可以为标记设置图标,标题等。但是我想在第一次发病时用标记显示标题。现在,仅当我点击标记时才显示标题。在v1中有叠加层,但是在v2中我没有发现任何类似的东西。编辑: 也许我不够清楚。Marker.showInfoWindow()API中的某些功能仅适用于一个标记。我无法同时显示所有标记的信息窗口。无论如何,我需要显示所有标记的标题,而无需等待用户点击它。
查看完整描述

3 回答

?
达令说

TA贡献1821条经验 获得超6个赞

终于做到了。因此,您要做的是有一个背景图像(在我的情况下,我仅使用一个蓝色矩形)。像这样创建一个标记:


Marker myLocMarker = map.addMarker(new MarkerOptions()

            .position(myLocation)

            .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.bluebox, "your text goes here"))));

请注意writeTextOnDrawable()方法:


private Bitmap writeTextOnDrawable(int drawableId, String text) {


    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)

            .copy(Bitmap.Config.ARGB_8888, true);


    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);


    Paint paint = new Paint();

    paint.setStyle(Style.FILL);

    paint.setColor(Color.WHITE);

    paint.setTypeface(tf);

    paint.setTextAlign(Align.CENTER);

    paint.setTextSize(convertToPixels(context, 11));


    Rect textRect = new Rect();

    paint.getTextBounds(text, 0, text.length(), textRect);


    Canvas canvas = new Canvas(bm);


    //If the text is bigger than the canvas , reduce the font size

    if(textRect.width() >= (canvas.getWidth() - 4))     //the padding on either sides is considered as 4, so as to appropriately fit in the text

        paint.setTextSize(convertToPixels(context, 7));        //Scaling needs to be used for different dpi's


    //Calculate the positions

    int xPos = (canvas.getWidth() / 2) - 2;     //-2 is for regulating the x position offset


    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.

    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;  


    canvas.drawText(text, xPos, yPos, paint);


    return  bm;

}




public static int convertToPixels(Context context, int nDP)

{

    final float conversionScale = context.getResources().getDisplayMetrics().density;


    return (int) ((nDP * conversionScale) + 0.5f) ;


}


查看完整回答
反对 回复 2019-12-25
  • 3 回答
  • 0 关注
  • 423 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信