25

我一直在试图添加一个可绘制的形状作为我要添加到地图上的标记的标记图标。Google Maps API v2 Android添加可绘制形状作为标记

形状看起来像这样(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)); 

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

标记图标必须是位图吗? 我可以添加形状drawables作为标记图标吗? 如果是的,我在做什么错?

回答

33

创建绘制你的标记(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/values/dimens.xml)中设置标记的大小:

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

我的方式来处理shape(xml)drawables作为标记图标(基于萨克斯曼答案)。

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

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

我的形状drawable具有设定的大小,因此,我可以查询绘制所需的位图大小。

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

当一个位图
相关问题