如何在android中的mapview的overlayitem上绘制独特的标签

发布于 2024-10-21 00:30:47 字数 163 浏览 10 评论 0原文

这是我试图实现的屏幕截图

在此处输入图像描述

我想添加独特的标签(例如数字)到地图的每个覆盖项目。我已经完成了添加覆盖项目并将其显示在地图上的基本部分。但这是我被困了很长时间的地方

Here is a screenshot of something which I'm trying to achieve

enter image description here

I would like to add unique label (like numbers) to each overlayitem of the map. I have done the basic part of adding the overlayitems and showing them on the map. But this where I am stuck for a long time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2024-10-28 00:30:47

您可以使用函数在同一个 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);

您将需要与最大标记数一样多的位图。但它比在位图上动态绘制文本要快。这是一部手机,处理器速度并不快。
如果您无论如何都喜欢在位图上绘制文本,那么非常简单,您可以这样做:

//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);

You can add OverlayItem with different markers on the same ItemizedOverlay by using function:

overlayItem.setMarker(drawable);

For this to work, you need to set the bounds on the 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);

You will need as much Bitmaps as your max number of markers. But it will be faster than dynamically draw text on bitmaps. It's a mobile phone, processors are not fast.
If you prefer to draw text on a Bitmap anyway, Ii's really easy, you can do it like this:

//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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文