谷歌地图API第2版的Andr​​oid添加形状绘制的标志形状、标志、地图、Andr

2023-09-05 00:32:32 作者:阿海

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

I've been trying to add a shape drawable as the marker icon for a marker I want to add on the map.

形状看起来像这样(RES /绘制/ blue_circle.xml):

shape looks like this (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>

和我尝试添加标记是这样的:

and I try to add the marker like this:

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

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

Apparently I get a NullPointer exception.

必须标记图标是一个位图? 我是允许添加的形状可绘制的标记图标? 如果是我究竟做错了什么?

Must the marker icon be a bitmap? Am I allowed to add shapes drawables as marker icons? And if yes what am I doing wrong?

推荐答案

为您标记创建一个可绘制(RES /绘制/ map_dot_red.xml):

Create a drawable for your marker (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>

创建绘制位图:

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

创建标记,使用位图:​​

Create your marker, using the bitmap:

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

设置你的标志物在梦诗大小(RES /价值/ dimens.xml):

Set the size of your marker in dimens (res/values/dimens.xml):

<resources>
    <dimen name="map_dot_marker_size">12dp</dimen>
</resources>
 
精彩推荐
图片推荐