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

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

    I will add a solution mixing severals ideas and codes from anywhere to fix this problem, first a function to manage image size:

    Future getBytesFromCanvas(double escala, urlAsset) async {
    
      final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
      final Canvas canvas = Canvas(pictureRecorder);
    
      final ByteData datai = await rootBundle.load(urlAsset);
      var imaged = await loadImage(new Uint8List.view(datai.buffer));
    
      double width = ((imaged.width.toDouble() * escala).toInt()).toDouble();
      double height = ((imaged.height.toDouble() * escala).toInt()).toDouble();
    
      canvas.drawImageRect(imaged, Rect.fromLTRB(0.0, 0.0, imaged.width.toDouble(), imaged.height.toDouble()),
                                  Rect.fromLTRB(0.0, 0.0, width, height),
                                  new Paint(),
      );
    
      final img = await pictureRecorder.endRecording().toImage(width.toInt(), height.toInt());
      final data = await img.toByteData(format: ui.ImageByteFormat.png);
      return data.buffer.asUint8List();
    
    }
    
    Future < ui.Image > loadImage(List < int > img) async {
      final Completer < ui.Image > completer = new Completer();
      ui.decodeImageFromList(img, (ui.Image img) {
    
        return completer.complete(img);
      });
      return completer.future;
    }
    

    Then apply this function depending on the device IOS or Android. The getBytesFromCanvas() function take two parameters, scale of image real size and asset url.

    var iconTour;
    
    bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
    if (isIOS){
    
      final markerIcon = await getBytesFromCanvas(0.7, 'images/Icon.png');
      iconTour = BitmapDescriptor.fromBytes(markerIcon);
    
    }
    else{
    
      final markerIcon = await getBytesFromCanvas(1, 'images/Icon.png');
      iconTour = BitmapDescriptor.fromBytes(markerIcon);
    
    }
    
    setState(() {
      final Marker marker = Marker(icon: iconTour);
    });
    

    Thats all.

提交回复
热议问题