【问题标题】:How to draw unique labels on overlayitem of mapview in android如何在android中mapview的overlayitem上绘制唯一标签
【发布时间】:2011-03-07 05:36:56
【问题描述】:

这是我正在努力实现的目标的屏幕截图

我想为地图的每个覆盖项添加唯一标签(如数字)。我已经完成了添加覆盖项并将它们显示在地图上的基本部分。但是这个我卡了很长时间

【问题讨论】:

    标签: android google-maps overlayitem


    【解决方案1】:

    您可以使用函数在同一个ItemizedOverlay上添加带有不同标记的OverlayItem

    overlayItem.setMarker(drawable);
    

    为此,您需要设置Drawable 的边界:

    Drawable icon1 = getResources().getDrawable(R.drawable.icon1);
    Drawable icon2 = getResources().getDrawable(R.drawable.icon2);
    icon1.setBounds(0, 0, icon1.getIntrinsicWidth(), icon1.getIntrinsicHeight());
    icon2.setBounds(0, 0, icon2.getIntrinsicWidth(), icon2.getIntrinsicHeight());
    OverlayItem item1 = new OverlayItem(new Point(48858290, 2294450), 
        "Tour Eiffel", "La tour Eiffel");
    OverlayItem item2 = new OverlayItem(new Point(48873830, 2294800), 
        "Arc de Triomphe", "L'arc de triomphe");                        
    item1.setMarker(icon1);
    item2.setMarker(icon2);
    

    您需要的位图数量与您的最大标记数一样多。但它会比在位图上动态绘制文本更快。这是一部手机,处理器并不快。 如果你更喜欢在 Bitmap 上绘制文本,Ii 真的很简单,你可以这样做:

    //get a reference on the ImageView 
    ImageView iv = (ImageView)findViewById(R.id.myImage);
    
    // load the marker image
    Bitmap myRefBitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.icon);
    
    // create a mutable bitmap with the same size as the marker image
    Bitmap myWrittenBitmap = Bitmap.createBitmap(myRefBitmap.getWidth(), 
        myRefBitmap.getHeight(), Bitmap.Config.ARGB_4444);
    
    // create a Canvas on which to draw and a Paint to write text.
    Canvas canvas = new Canvas(myWrittenBitmap);
    Paint txtPaint = new Paint();
    txtPaint.setColor(Color.RED);
    txtPaint.setTextSize(12);
    txtPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    txtPaint.setTypeface(Typeface.DEFAULT_BOLD);
    
    //draw ref bitmap then text on our canvas
    canvas.drawBitmap(myRefBitmap, 0, 0, null);
    canvas.drawText("Droid", 5, 15, txtPaint);
    
    // set the new written bitmap into the ImageView
    iv.setImageBitmap(myWrittenBitmap);
    

    【讨论】:

    • 但我只想在标记上添加标签。我可以有100多个标记。添加这么多标记作为资源似乎不是一个好主意
    • 我编辑了答案并添加了在位图上绘制文本的方式。但我真的认为在移动平台上预编译资源比动态生成的资源更好。
    • 感谢您的回复,但正如您所建议的那样,从可绘制对象创建位图,反之亦然,成本确实很高。相反,我正在考虑重写 ItemizedOverlay 类的 onDraw 方法,遍历其所有项目,然后相应地绘制文本。
    • 位图生成后,默认的onDraw行为会使用缓存,如果真的要自己绘制文字,最好在onCreate方法中进行。
    猜你喜欢
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 2012-09-07
    相关资源
    最近更新 更多