AssetMapBitmap constructor

AssetMapBitmap(
  1. String assetName, {
  2. MapBitmapScaling bitmapScaling = MapBitmapScaling.auto,
  3. double? imagePixelRatio,
  4. double? width,
  5. double? height,
})

Creates a AssetMapBitmap from an asset image.

To create an instance of AssetMapBitmap from mipmapped assets, use the asynchronous AssetMapBitmap.create method instead of this constructor.

The imagePixelRatio parameter allows to give correct pixel ratio of the asset image. If the imagePixelRatio is not provided, value is defaulted to the natural resolution of 1.0. To render the asset as sharp as possible, set the imagePixelRatio to the devices pixel ratio.

bitmapScaling controls the scaling behavior:

  • MapBitmapScaling.auto automatically upscales and downscales the image to match the device's pixel ratio or the specified dimensions, maintaining consistency across devices.
  • MapBitmapScaling.none disables automatic scaling, which is useful when performance is a concern or if the asset is already scaled appropriately.

Optionally, width and height can be specified to control the dimensions of the rendered image:

  • If both width and height are non-null, the image will have the specified dimensions, which might distort the original aspect ratio, similar to BoxFit.fill.
  • If only one of width and height is non-null, then the output image will be scaled to the associated width or height, and the other dimension will take whatever value is needed to maintain the image's original aspect ratio. These cases are similar to BoxFit.fitWidth and BoxFit.fitHeight, respectively.

If width or height is provided, imagePixelRatio value is ignored.

The following example demonstrates how to create an AssetMapBitmap from an asset image without automatic asset resolving:

AssetMapBitmap mapBitmap = AssetMapBitmap(
  'assets/images/map_icon.png',
  bitmapScaling: MapBitmapScaling.auto,
  width: 40, // Desired width in logical pixels.
);

To render the bitmap as sharply as possible, set the imagePixelRatio to the device's pixel ratio. This renders the asset at a pixel-to-pixel ratio on the screen, but may result in different logical marker sizes across devices with varying pixel densities.

AssetMapBitmap assetMapBitmap = AssetMapBitmap(
  'assets/images/map_icon.png',
  imagePixelRatio: MediaQuery.maybeDevicePixelRatioOf(context),
);

Implementation

AssetMapBitmap(
  String assetName, {
  MapBitmapScaling bitmapScaling = MapBitmapScaling.auto,
  double? imagePixelRatio,
  double? width,
  double? height,
}) : this._(
        assetName: assetName,
        bitmapScaling: bitmapScaling,
        imagePixelRatio: imagePixelRatio ?? _naturalPixelRatio,
        width: width,
        height: height,
      );