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

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

    I found simplest way to solve this issue.

    I used below version for google map implementation. In lower version of google map BitmapDescriptor.fromBytes not working.

     google_maps_flutter: ^0.5.19
    

    And set marker points like

    Future setMarkersPoint() async {
      var icon = 'your url';
      Uint8List dataBytes;
      var request = await http.get(icon);
      var bytes = await request.bodyBytes;
    
      setState(() {
        dataBytes = bytes;
      });
    
      final Uint8List markerIcoenter code heren =
          await getBytesFromCanvas(150, 150, dataBytes);
    
      var myLatLong = LatLng(double.parse(-6.9024812),
          double.parse(107.61881));
    
      _markers.add(Marker(
        markerId: MarkerId(myLatLong.toString()),
        icon: BitmapDescriptor.fromBytes(markerIcon),
        position: myLatLong,
       infoWindow: InfoWindow(
         title: 'Name of location',
        snippet: 'Marker Description',
       ),
      ));
    

    }

    And If you want to change icon size then use below code.

    Future getBytesFromCanvas(
      int width, int height, Uint8List dataBytes) async {
    final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
    final Canvas canvas = Canvas(pictureRecorder);
    final Paint paint = Paint()..color = Colors.transparent;
    final Radius radius = Radius.circular(20.0);
    canvas.drawRRect(
        RRect.fromRectAndCorners(
          Rect.fromLTWH(0.0, 0.0, width.toDouble(), height.toDouble()),
          topLeft: radius,
          topRight: radius,
          bottomLeft: radius,
          bottomRight: radius,
        ),
        paint);
    
    var imaged = await loadImage(dataBytes.buffer.asUint8List());
    canvas.drawImageRect(
      imaged,
      Rect.fromLTRB(
          0.0, 0.0, imaged.width.toDouble(), imaged.height.toDouble()),
      Rect.fromLTRB(0.0, 0.0, width.toDouble(), height.toDouble()),
      new Paint(),
    );
    
        final img = await pictureRecorder.endRecording().toImage(width, height);
        final data = await img.toByteData(format: ui.ImageByteFormat.png);
        return data.buffer.asUint8List();
     }
    
        Future loadImage(List img) async {
        final Completer completer = new Completer();
        ui.decodeImageFromList(img, (ui.Image img) {
      return completer.complete(img);
    });
    return completer.future;
    }
    

    Hope It will work for you..!!

提交回复
热议问题