Android Google Maps API v2 - how to change marker icon

后端 未结 8 1310
青春惊慌失措
青春惊慌失措 2021-01-31 16:34

I am trying to change marker icon. I get the image from one server directory.

When I put break point every time the \"bit\" result is null. And when I run t

8条回答
  •  南旧
    南旧 (楼主)
    2021-01-31 16:56

    Use .icon() Add like this

    Marker marker = map.addMarker(new MarkerOptions().position(currentLocation)
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_place_holder)));
    

    Please note don't use vector Images, if you want to use vector use below code

    private BitmapDescriptor bitmapDescriptorFromVector(Context context, @DrawableRes  int vectorDrawableResourceId) {
        Drawable background = ContextCompat.getDrawable(context, R.drawable.ic_map_pin_filled_blue_48dp);
        background.setBounds(0, 0, background.getIntrinsicWidth(), background.getIntrinsicHeight());
        Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorDrawableResourceId);
        vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() + 40, vectorDrawable.getIntrinsicHeight() + 20);
        Bitmap bitmap = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        background.draw(canvas);
        vectorDrawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }
    

提交回复
热议问题