自定义图标方向锚点抖动Google Map

时间:2019-09-11 20:38:54

标签: google-maps flutter

我使用此代码为Flutter Google Map标记创建自定义图标

createMarker(){
    final MarkerId markerId = getMarkerId();
      markers[markerId] = Marker(
        rotation: driverLocation.orientation,
        markerId: markerId,
        icon: BitmapDescriptor.fromBytes(await getBytesFromAsset('assets/icons/taxi-icon.png', 50);),
        position: position.toLatLng(),
      );
}
 Future<Uint8List> getBytesFromAsset(String path, int width) async {
    ByteData data = await rootBundle.load(path);
    ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List(), targetWidth: width);
    ui.FrameInfo fi = await codec.getNextFrame();
    return (await fi.image.toByteData(format: ui.ImageByteFormat.png)).buffer.asUint8List();
  }

它工作正常,但是我按照方向进行了测试,发现标记的锚点似乎在图像的顶部边缘,而不是在图像的中心。如何将锚点更改为图像的中心?

1 个答案:

答案 0 :(得分:2)

尝试将锚点参数添加到标记。

Marker(
   ...
   anchor: const Offset(0.5, 0.5)
);

默认值:

anchor: const Offset(0.5, 1.0)

要将锚点设置到图标的中心:

anchor: const Offset(0.5, 0.5)

要将锚点设置到图标的左上角:

anchor: const Offset(0.0, 0.0)

要将锚点设置到图标的右下角:

anchor: const Offset(1.0, 1.0)