createCustomMarkerBitmap function
Future<BitmapDescriptor>
createCustomMarkerBitmap(
- String title, {
- required TextStyle textStyle,
- bool visible = true,
- Color backgroundColor = Colors.blueAccent,
})
Implementation
Future<BitmapDescriptor> createCustomMarkerBitmap(String title,
{required TextStyle textStyle,
bool visible = true,
Color backgroundColor = Colors.blueAccent}) async {
// title = "test dure label";
TextSpan span = TextSpan(
style: textStyle,
text: title,
);
TextPainter painter = TextPainter(
text: span,
textAlign: TextAlign.center,
textDirection: ui.TextDirection.ltr,
);
painter.text = TextSpan(
text: title.toString(),
style: textStyle,
);
ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
Canvas canvas = Canvas(pictureRecorder);
painter.layout();
painter.paint(canvas, const Offset(20.0, 10.0));
int textWidth = painter.width.toInt();
int textHeight = painter.height.toInt();
canvas.drawRRect(
RRect.fromLTRBAndCorners(0, 0, textWidth + 40, textHeight + 20,
bottomLeft: const Radius.circular(10),
bottomRight: const Radius.circular(10),
topLeft: const Radius.circular(10),
topRight: const Radius.circular(10)),
Paint()..color = backgroundColor);
var arrowPath = Path();
arrowPath.moveTo((textWidth + 40) / 2 - 15, textHeight + 20);
arrowPath.lineTo((textWidth + 40) / 2, textHeight + 40);
arrowPath.lineTo((textWidth + 40) / 2 + 15, textHeight + 20);
arrowPath.close();
canvas.drawPath(arrowPath, Paint()..color = backgroundColor);
painter.layout();
painter.paint(canvas, const Offset(20.0, 10.0));
// ui.Picture p = pictureRecorder.endRecording();
// ByteData? pngBytes = await (await p.toImage(
// painter.width.toInt() + 40, painter.height.toInt() + 50))
// .toByteData(format: ui.ImageByteFormat.png);
// Uint8List data = Uint8List.view(pngBytes!.buffer);
// 绘制BitmapDescriptor.defaultMarker
// BitmapDescriptor defaultMarker = BitmapDescriptor.defaultMarker;
// Uint8List defaultMarkerData = await defaultMarker.toBytes();
ByteData byteData = await rootBundle.load("images/location.png");
Uint8List defaultMarkerData = await byteData.buffer.asUint8List();
if (visible){
// 将自定义图像和BitmapDescriptor.defaultMarker进行叠加
int width = painter.width.toInt() + 40;
int height = painter.height.toInt() + 50;
ui.Picture p = pictureRecorder.endRecording();
ByteData? pngBytes = await (await p.toImage(width, height)).toByteData(format: ui.ImageByteFormat.png);
Uint8List customImageData = Uint8List.view(pngBytes!.buffer);
// 创建一个新的Canvas并绘制自定义图像和BitmapDescriptor.defaultMarker
final recorder = ui.PictureRecorder();
final canvas2 = Canvas(recorder);
final customImage = await decodeImageFromList(customImageData);
final defaultMarkerImage = await decodeImageFromList(defaultMarkerData);
canvas2.drawImage(customImage, Offset.zero, Paint());
canvas2.drawImage(defaultMarkerImage, Offset(customImage.width.toDouble() / 2 - defaultMarkerImage.width.toDouble() / 2 , customImage.height.toDouble()), Paint());
ui.Picture picture = recorder.endRecording();
int mergeImageWidth = customImage.width;
if (customImage.width < defaultMarkerImage.width){
mergeImageWidth = defaultMarkerImage.width;
}
// 将叠加后的图像转换为BitmapDescriptor
ByteData? mergedBytes = await (await picture.toImage(mergeImageWidth, customImage.height + defaultMarkerImage.height)).toByteData(format: ui.ImageByteFormat.png);
Uint8List mergedData = mergedBytes!.buffer.asUint8List();
return BitmapDescriptor.fromBytes(mergedData);
}else{
return BitmapDescriptor.fromBytes(defaultMarkerData);
}
// return BitmapDescriptor.fromBytes(data);
// return BitmapDescriptor.defaultMarker;
}