Google Maps API v2 Android add shape drawable as marker

后端 未结 3 1264
不知归路
不知归路 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条回答
  •  旧时难觅i
    2020-12-14 16:15

    Create a drawable for your marker (res/drawable/map_dot_red.xml):

    
    
    
        
    
        
    
    
    

    Create a bitmap from the 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);
    

    Create your marker, using the bitmap:

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

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

    
        12dp
    
    

提交回复
热议问题