为Android版谷歌地图标记使用xml布局 [英] Using an xml Layout for a Google Map Marker for Android

查看:154
本文介绍了为Android版谷歌地图标记使用xml布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用布局而不是我的标记我使用Google Maps Api 2作为Android应用。

I want to use a layout and not a drawable for my marker I am using with Google Maps Api 2 for an Android app.

喜欢这样:

  Marker m = googleMap
                        .addMarker(new MarkerOptions()
                                .position(LOC)
                                .snippet(user)
                                .title(name)
                                .icon(BitmapDescriptorFactory.fromResource(R.layout.map_marker)));

但是,它似乎只是为了使用 drawable 文件夹。

However, it seems this is only designed for use from drawable folder.

我只是带有背景的图像,我可以根据标记类型更改颜色。这是我想要使用的布局:

I simply what an image with a background where I can change colors based on marker type. This is the layout I want to use:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="#222"
    android:id="@+id/llMarker">

    <ImageView
        android:id="@+id/ivMarker"
        android:layout_marginTop="5dp"
        android:layout_width="40dp"
        android:layout_height="40dp" />

</LinearLayout>

非常基本。我如何将其用作标记? (或者至少复制我想要的最终结果?)

Pretty basic. How can I use this as a marker? (or at least replicate the end result that I am going for?)

文件说你可以使用这个:

The documents say you can use this:

fromResource (int resourceId)

所以我假设了布局可能有效。

So I assumed a layout might work.

更新:

我从答案中采用了这个概念下面并重做一些事情;这仍然不起作用;没有错误。只是不会显示图像。

I have taken the concept from answer below and reworked some things; this still is not working; no errors. Just will not show image.

        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.map_marker, null);

        m = googleMap
                .addMarker(new MarkerOptions()
                        .position(LOC)
                        .snippet(user)
                        .title(name)
                        .icon(BitmapDescriptorFactory.fromBitmap(loadBitmapFromView(v))));

//方法

  public static Bitmap loadBitmapFromView(View v) {

        if (v.getMeasuredHeight() <= 0) {
            v.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
            Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
            v.draw(c);
            return b;
        }
        return null;
    }


推荐答案

你不能直接使用布局使用 BitmapDescriptorFactory.fromResource()

You cannot directly use a layout with BitmapDescriptorFactory.fromResource():


使用资源ID创建一个BitmapDescriptor 图片

但是,您可以将布局绘制到位图中,然后使用它。您需要:

However, you could draw the layout into a bitmap, and then use that. You'd need to:


  • 从xml中扩充布局(使用 LayoutInflater )。

  • 更改您想要的任何属性(例如背景颜色)。

  • 将此布局渲染为位图,通过 Canvas ,例如描述这里

  • 将此位图传递到 BitmapDescriptorFactory.fromBitmap()

  • Inflate the layout from xml (with a LayoutInflater).
  • Change whatever properties you want (e.g. background color).
  • Render this layout into a Bitmap, via Canvas, for example as described here.
  • Pass this bitmap into BitmapDescriptorFactory.fromBitmap().

这篇关于为Android版谷歌地图标记使用xml布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆