101: As long as are able to encode any image into raw bytes such as 100, you should be fine using it as a marker.
到目前为止,您可以使用Uint8List
个数据通过Google Maps创建您的标记.这意味着只要您保持正确的编码格式(在此特定场景中,编码格式为png
),您就可以使用raw数据绘制任何您想要的 map 标记.
我将介绍两个示例,在这两个示例中,您可以执行以下任一操作:
- Select 一个本地资源,并动态地将其大小更改为您想要的任何大小,并将其渲染到 map 上(一个Flutter 的徽标图像);
- 在画布上绘制一些东西,并将其渲染为标记,但这可以是any个渲染小部件.
除此之外,您甚至可以转换静电图像中的呈现小工具,因此也可以将其用作标记.
1.使用assets资源
首先,创建一个处理资源路径并接收大小的方法(可以是宽度、高度或两者,但只使用一个将保留比率).
import 'dart:ui' as ui;
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();
}
然后,只需使用正确的描述符将其添加到 map 中:
final Uint8List markerIcon = await getBytesFromAsset('assets/images/flutter.png', 100);
final Marker marker = Marker(icon: BitmapDescriptor.fromBytes(markerIcon));
这将分别为50、100和200宽度生成以下内容.
个
2.使用画布
你可以用画布画任何你想画的东西,然后用它做记号.下面将生成一些简单的圆形框,其中包含Hello world!
个文本.
首先,用画布画一些东西:
Future<Uint8List> getBytesFromCanvas(int width, int height) async {
final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
final Canvas canvas = Canvas(pictureRecorder);
final Paint paint = Paint()..color = Colors.blue;
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);
TextPainter painter = TextPainter(textDirection: TextDirection.ltr);
painter.text = TextSpan(
text: 'Hello world',
style: TextStyle(fontSize: 25.0, color: Colors.white),
);
painter.layout();
painter.paint(canvas, Offset((width * 0.5) - painter.width * 0.5, (height * 0.5) - painter.height * 0.5));
final img = await pictureRecorder.endRecording().toImage(width, height);
final data = await img.toByteData(format: ui.ImageByteFormat.png);
return data.buffer.asUint8List();
}
然后以同样的方式使用它,但这一次提供您想要的任何数据(例如,宽度和高度),而不是资源路径.
final Uint8List markerIcon = await getBytesFromCanvas(200, 100);
final Marker marker = Marker(icon: BitmapDescriptor.fromBytes(markerIcon));
这就是你要的.
个