在 MapView 上使用 Layout 作为 OverlayItem?

发布于 2024-09-10 20:28:40 字数 256 浏览 4 评论 0 原文

是否可以使用 XML 中定义的布局作为要添加到 MapView 的 OverlayItem 而不仅仅是单个可绘制对象?我在地图上绘制了几个叠加层,但它们只是单个图像,我现在想放置更复杂的对象的叠加层,而不仅仅是一个简单的可绘制对象。

我计划绘制一个标记,其中包含动态图像(只是朋友定位器类型的东西),其中每个标记内部都有不同的图片。

将这些添加为 OverlayItems 是解决问题的方法还是我需要将每个项目作为子视图添加到 MapView 中(这似乎效率低下)?

Is it possible to use a layout defined in XML as an OverlayItem to be added to a MapView instead of just a single drawable? I have several Overlays being drawn on the map but they are just single images, I would now like to place Overlays that are more complex objects instead of just a simple drawable.

I am planning on drawing a marker with a dynamic image inside it (just a friend locator type thing), where each marker has a different picture inside.

Is adding these as OverlayItems the way to go about it or do I need to add each one to the MapView as a child view (this seems like it would be inefficient)?

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

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

发布评论

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

评论(2

握住我的手 2024-09-17 20:28:40

我试图做类似的事情 - 将带有自定义视图的 Google 地图风格气球绘制到地图上。我尝试重写 OverlayItem 的 draw() 方法,并以这种方式绘制我在 XML 中定义的布局。问题在于将视图转换为可绘制对象以绘制到 MapView 画布上,这意味着我失去了布局的所有事件处理功能,因此按钮等内容无法正常工作。如果您不需要担心图像上的任何事件,那么这可以工作,您只需要使用当前 Activity 的 LayoutInflater (activity.getLayoutInflater()) 来膨胀您的视图,按照下面的描述进行测量和布局。 Android 开发者网站上的“Android 如何绘制视图”页面(由于新的用户限制,无法发布链接,抱歉!),最后在视图上调用 buildDrawingCache() 和 getDrawingCache() 将其获取为位图。然后可以通过您构建的 Overlay 子类的 draw() 方法将其绘制到 Canvas 上。

我实际上最终采用了您建议的第二种方法(将每个视图作为子视图添加到 MapView,使用 MapView.LayoutParams 将布局悬停在相关的 OverlayItem 上)。不过,我只有一个子视图需要担心,所以我不太担心这里的效率,如果这实际上是值得关注的事情,这对你来说可能是一个更大的问题(不过,它可能值得首先测试)。有一小段关于我所做的事情

最后,您可能想看看 android-mapviewballoons 的方法,这是非常值得一试的看。

I was trying to do something similar - draw Google Maps style balloons with custom views inside them onto the map. I tried overriding the draw() of an OverlayItem, and drawing the layout I'd defined in XML that way. The problem was in coverting the View to a Drawable to draw onto the MapView canvas meant I lost all event handling capabilities of the Layout so things like buttons etc. did not work properly. If you don't have any events on the images to worry about then this could work, you'll just need to inflate your view using the current Activity's LayoutInflater (activity.getLayoutInflater()), measure and lay it out as described under the 'How Android Draws Views' page on the android developers site (couldn't post a link due to new user restrictions, sorry!), and finally call buildDrawingCache() and getDrawingCache() on your View to get it as a Bitmap. This can then be drawn onto the Canvas passed the draw() method of a subclass of Overlay that you've built.

I actually went for the second approach you suggested (adding each one as a child view to MapView, using MapView.LayoutParams to hover the layout over the OverlayItem in question) in the end. I only had one child view to worry about though so I wasn't too worried about the efficiency here, it might be more of a problem to you if that is actually something to be concerned about (it might be worth testing first though). There's a small writeup of what I did here with a bit more detail.

Finally you might want to look at android-mapviewballoons's approach to this, which is well worth a look.

单身狗的梦 2024-09-17 20:28:40

通过将布局转换为可绘制对象,我可以将布局添加为地图上的标记。这是一个来自 bitmapFromView 函数>本教程

public static Bitmap bitmapFromView(View layout, int width, int height, Context context) {
    // Create a new bitmap and a new canvas using that bitmap
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    layout.setDrawingCacheEnabled(true);
    // Supply measurements
    layout.measure(View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY));
    // Apply the measures so the layout would resize before drawing.
    layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
    // and now the bmp object will actually contain the requested layout
    canvas.drawBitmap(layout.getDrawingCache(), 0, 0, new Paint());
    return bmp;
}

我使用该方法将位图添加为标记,如下所示:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.MY_LAYOUT, null);
Bitmap markerBitmap = bitmapFromView(layout, MY_WIDTH, MY_HEIGHT, context);

MarkerOptions markerOptions = new MarkerOptions()
            .title(MY_TITLE)
            .position(MY_POSITION)
            .icon(BitmapDescriptorFactory.fromBitmap(markerBitmap));
map.addMarker(markerOptions);

I was able to add a layout as a Marker on a map by converting the layout to a drawable. Here's a bitmapFromView function taken from this tutorial:

public static Bitmap bitmapFromView(View layout, int width, int height, Context context) {
    // Create a new bitmap and a new canvas using that bitmap
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    layout.setDrawingCacheEnabled(true);
    // Supply measurements
    layout.measure(View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY));
    // Apply the measures so the layout would resize before drawing.
    layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
    // and now the bmp object will actually contain the requested layout
    canvas.drawBitmap(layout.getDrawingCache(), 0, 0, new Paint());
    return bmp;
}

I use the method to add the bitmap as a Marker like this:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.MY_LAYOUT, null);
Bitmap markerBitmap = bitmapFromView(layout, MY_WIDTH, MY_HEIGHT, context);

MarkerOptions markerOptions = new MarkerOptions()
            .title(MY_TITLE)
            .position(MY_POSITION)
            .icon(BitmapDescriptorFactory.fromBitmap(markerBitmap));
map.addMarker(markerOptions);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文