ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

为Android版Google地图标记使用xml布局

2019-07-17 17:34:42  阅读:157  来源: 互联网

标签:android layout imageview google-maps-android-api-2


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

像这样:

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

但是,它似乎只是设计用于drawable文件夹.

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

<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>

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

文件说你可以用这个:

fromResource (int resourceId)

所以我假设布局可能有效.

更新:

我从下面的答案中采用了这个概念并重新设计了一些东西;这还不行;没有错误.只是不会显示图像.

        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()的布局:

Creates a BitmapDescriptor using the resource id of an image.

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

>从xml(使用LayoutInflater)扩展布局.
>更改所需的任何属性(例如背景颜色).
>通过Canvas将此布局渲染为位图,例如here所述.
>将此位图传递到BitmapDescriptorFactory.fromBitmap().

标签:android,layout,imageview,google-maps-android-api-2
来源: https://codeday.me/bug/20190717/1490756.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有