Google Maps API v2 Android add shape drawable as marker

后端 未结 3 1262
不知归路
不知归路 2020-12-14 15:50

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

shape looks like this (res/drawable/blue_circle.xml):

<         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-14 16:17

    Here's an answer if you're using the com.google.maps.android:android-maps-utils library.

    Create a Bitmap using 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();
    

    Then use the bitmap like you would when setting the marker icon BitmapDescriptorFactory.fromBitmap(bitmap)

提交回复
热议问题