-->

谷歌地图API第2版的Android添加形状绘制的标记(Google Maps API v2 And

2019-07-19 01:39发布

我一直在试图增加一个形状绘制作为标记我想在地图上添加标记图标。

形状是这样的(RES /抽拉/ 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));

显然,我得到一个空指针异常。

必须标记图标是一个位图? 我可以在添加图形绘制资源作为标记图标? 如果是我在做什么错?

Answer 1:

为您的标记(RES /绘制/ 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>

创建一个从绘制一个位图:

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

设置你的标记在梦诗(RES /价值/ dimens.xml)尺寸:

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


Answer 2:

下面是如果你使用的答案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)



Answer 3:

我对付形状(XML)的方式绘项目为标记图标(基于萨克斯曼答案)。

我有很多的对象(标志),以在地图上显示。 他们有两种类型的可绘制的 - 位图和形状。 相同的图标我用于多个标记。 因此,我已经添加了一些缓存位图描述符和抽拉型的检测。

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>


文章来源: Google Maps API v2 Android add shape drawable as marker