Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use BitmapDescriptor.fromAssetImage() to set a custom Marker icon?

I'm trying to set a custom icon for the markers of my map, in my Flutter app. Since BitmapDescriptor.fromAsset() is deprecated, I'm sruggling to find how to use BitmapDescriptor.fromAssetImage().

I did not find any doc or Stackoverflow question about this.

This is the current way I'm creating it (without the custom image):

Marker(
          markerId: MarkerId(pos.toString()),
          position: pos,
          infoWindow: InfoWindow(
            title: store['store_name'],
            snippet: '',
          ),
          icon: BitmapDescriptor.defaultMarker));
like image 275
Mickaël D Avatar asked May 22 '19 15:05

Mickaël D


1 Answers

Define a field in Class:

BitmapDescriptor myIcon;

Retrieve icon before the Map is ready.

@override
void initState() {
    BitmapDescriptor.fromAssetImage(
        ImageConfiguration(size: Size(48, 48)), 'assets/my_icon.png')
        .then((onValue) {
      myIcon = onValue;
    });
 }

set the Icon:

icon: myIcon;

Make sure you have set the icon in Flutter section of pubspec.yaml

 assets:
    - assets/my_icon.png
like image 80
Akshar Patel Avatar answered Sep 20 '22 21:09

Akshar Patel