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

前端 未结 14 617
不知归路
不知归路 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:13

    Since google_map_flutter 0.5.26, fromAsset() is deprecated and should be replaced with fromAssetImage() as some other answers mentioned. A more elegant way to apply fromAssetImage() for different resolution devices is to declare resolution-aware image assets. The idea is that Flutter renders screens using logical pixel, which is around 72px per inch if I remember correctly, while modern mobile devices could contain more than 200px per inch. And the solution to make a image looks similar in size on different mobile devices with different pixel density is to prepare multiple copy of the same image in different size, where on lower pixel density device the smaller image is used, and on higher pixel density device the bigger image is used.

    So you should prepare for example the following images

    images/car.png           <-- if this base image is 100x100px
    images/2.0x/car.png      <-- 2.0x one should be 200x200px
    images/3.0x/car.png      <-- and 3.0x one should be 300x300px
    

    and modify your code as below, where createLocalImageConfiguration() will apply the correct scale according to devicePixelRatio

    mapController.addMarker(
            MarkerOptions(
              icon: BitmapDescriptor.fromAssetImage(
                      createLocalImageConfiguration(context),
                      "images/car.png"),
              position: LatLng(
                deviceLocations[i]['latitude'],
                deviceLocations[i]['longitude'],
              ),
            ),
          );
    

    Below is the implementation of fromAssetImage() of the latest google_map_flutter 1.0.3. You can see that the underlying implementation of BitmapDescriptor takes an argument scale, which is the key to getting the right size of image.

      static Future fromAssetImage(
        ImageConfiguration configuration,
        String assetName, {
        AssetBundle bundle,
        String package,
        bool mipmaps = true,
      }) async {
        if (!mipmaps && configuration.devicePixelRatio != null) {
          return BitmapDescriptor._([
            'fromAssetImage',
            assetName,
            configuration.devicePixelRatio,
          ]);
        }
        final AssetImage assetImage =
            AssetImage(assetName, package: package, bundle: bundle);
        final AssetBundleImageKey assetBundleImageKey =
            await assetImage.obtainKey(configuration);
        return BitmapDescriptor._([
          'fromAssetImage',
          assetBundleImageKey.name,
          assetBundleImageKey.scale,
          if (kIsWeb && configuration?.size != null)
            [
              configuration.size.width,
              configuration.size.height,
            ],
        ]);
      }
    

    NOTE: You can see that the size property of the ImageConfiguration only works for web.

提交回复
热议问题