How to change the icon size of Google Maps marker in Flutter?

前端 未结 14 785
不知归路
不知归路 2020-12-07 12:32

I am using google_maps_flutter in my flutter app to use google map I have custom marker icon and I load this with BitmapDescriptor.fromAsset(\"images/car.

14条回答
  •  不知归路
    2020-12-07 13:15

    Large images should be avoided, as they consume unnecessary space. Images should be scaled for your map, with variations of pixel resolution to cater for the device.

    For example the base image should be scaled to the correct size outside of your application. Different devices have different pixel resolutions, which flutter caters for. Different version of your image are required so that the image does not appear jagged. Scale up the image for different resolutions. i.e base version 32x32 pixels, version 2.0 will be 64x64 pixels, version 3.0 will be 128x128 etc. See the standard flutter way described below, which caters for different pixel resolutions, dependent on the device manufacturer.

    BitmapDescriptor.fromAsset does not support the automatic decoding of pixel resolution, and will load the file specified in the path. To correct this call AssetImage to decode the correct filename.

    There is a bug with the rendering of images, images in iOS look bigger than Android, see defect 24865. There is a workaround for this too, by hardcoding the file name of the resolution you would prefer.

    The following sections outline the standard flutter way, the AssetImage workaround, and the 24865 workaround.

    Standard Flutter image naming conventions

    Create an asset folder with the naming convention:

    pathtoimages/image.png
    pathtoimages/Mx/image.png
    pathtoimages/Nx/image.png
    pathtoimages/etc.
    

    Where M and N are resolutions (2.0x) or themes (dark). Then add the image or all the images to the pubspec.file as either

    flutter:
      assets:
        - pathtoimages/image.png
    

    or

    flutter:
      assets:
        - pathtoimages/
    

    Workaround for Google Maps

    This standard requires that images are then loaded using AssetImage('pathtoimages/image.png') which is not supported by the google maps plugin. Google maps requires that you use BitmapDescriptor.fromAsset('pathtoimages/image.png'), which at this time does not resolve to the correct image. To fix this use you can get the correct image from AssetImage by first createLocalImageConfiguration using the BuildContext as defined here. Then use this configuration to resolve the correct image as follows:

    ImageConfiguration config = createLocalImageConfiguration(context);
    AssetImage('pathtoimages/image.png')
       .obtainKey(config)
       .then((resolvedImage) {
           print('Name: ' + resolvedImage.onValue.name);
        });
    

    Defect 24865 workaround

     BitmapDescriptor get deliveryIcon {
          bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
              If (isIOS)
                  return BitmapDescriptor.fromAsset('pathtoimages/image.png');
             else
                  return BitmapDescriptor.fromAsset(
                  resolvedImageName);
        }
    

提交回复
热议问题