谷歌地图API v2 Android 添加形状可绘制物作为标记

30

我一直在尝试将一个形状可绘制对象作为标记图标添加到地图上的标记中。

这个形状看起来像这样(res/drawable/blue_circle.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <size
        android:width="15dp"
        android:height="15dp" />
    <solid
        android:color="@color/Blue" />
</shape>

我尝试像这样添加标记:

markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.blue_circle));

显然我收到了一个NullPointer异常。

标记图标必须是位图吗? 我可以将形状绘制添加为标记图标吗? 如果可以的话,我做错了什么?

3个回答

39

为您的标记创建一个可绘制对象 (res/drawable/map_dot_red.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <gradient
        android:angle="90"
        android:endColor="#f58383"
        android:startColor="#ee6464" />

    <stroke
        android:width="1dp"
        android:color="#a13939" />

</shape>

从Drawable创建位图:

int px = getResources().getDimensionPixelSize(R.dimen.map_dot_marker_size);
mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mDotMarkerBitmap);
Drawable shape = getResources().getDrawable(R.drawable.map_dot_red);
shape.setBounds(0, 0, mDotMarkerBitmap.getWidth(), mDotMarkerBitmap.getHeight());
shape.draw(canvas);

使用位图创建标记:

Marker marker = mMap.addMarker(new MarkerOptions()
    .position(point)
    .anchor(.5f, .5f)
    .icon(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap)));

在dimens(res/values/dimens.xml)中设置您的标记大小:

<resources>
    <dimen name="map_dot_marker_size">12dp</dimen>
</resources>

3
如果您正在使用“com.google.maps.android:android-maps-utils”库,则可以按照以下方式进行操作。使用“IconGenerator”创建位图。
IconGenerator iconGen = new IconGenerator(context);

// Define the size you want from dimensions file
int shapeSize = getResources().getDimensionPixelSize(R.dimen.shape_size);

Drawable shapeDrawable = ResourcesCompat.getDrawable(getResources(), R.drawable.my_shape, null);
iconGen.setBackground(shapeDrawable);

// Create a view container to set the size
View view = new View(context);
view.setLayoutParams(new ViewGroup.LayoutParams(shapeSize, shapeSize));
iconGen.setContentView(view);

// Create the bitmap
Bitmap bitmap = iconGen.makeIcon();

然后像设置标记图标一样使用位图 BitmapDescriptorFactory.fromBitmap(bitmap)

。这里的“位图”指的是之前创建的图像文件,可通过此方法将其转换为标记图标。


0

我的处理形状(xml)可绘制物作为标记图标的方式(基于saxman的答案)。

我有很多对象(标记)要显示在地图上。它们具有位图和形状两种类型的可绘制物。同一图标可能会用于多个标记。因此,我为位图描述符和可绘制类型检测添加了一些缓存。

SparseArray<BitmapDescriptor> iconCache = new SparseArray<>();
for (MyObject object : objects) {
    int iconResId = object.icon;

    BitmapDescriptor icon = iconCache.get(iconResId);
    if (icon == null) {
        Drawable drawable;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            drawable = getResources().getDrawable(iconResId, null);
        } else {
            drawable = getResources().getDrawable(iconResId);
        }
        if (drawable instanceof GradientDrawable) {
            Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
            drawable.draw(canvas);
            icon = BitmapDescriptorFactory.fromBitmap(bitmap);
            bitmap.recycle();
        } else {
            icon = BitmapDescriptorFactory.fromResource(iconResId);
        }
        iconCache.put(iconResId, icon);

        map.addMarker(new MarkerOptions()
            .icon(icon)
            .position(position));
    }
}

我的形状可绘制对象已经设置了大小,因此,我可以查询绘制对象所需的位图大小。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size android:width="12dp" android:height="12dp" />
    <solid android:color="@color/transit_bus" />
</shape>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接