gpt4 book ai didi

flutter - 如何使用 google_maps_flutter 在标记内创建文本?

转载 作者:行者123 更新时间:2023-12-05 00:41:54 26 4
gpt4 key购买 nike

如何在谷歌地图上实现一个标记,标记上的文本会 flutter 。图片供引用:

/image/W6oqG.jpg

最佳答案

我用这个方法解决了

Future<BitmapDescriptor> createCustomMarkerBitmap(String title) async {
TextSpan span = new TextSpan(
style: new TextStyle(
color: Prefs.singleton().getTheme() == 'Dark'
? Colors.white
: Colors.black,
fontSize: 35.0,
fontWeight: FontWeight.bold,
),
text: title,
);

TextPainter tp = new TextPainter(
text: span,
textAlign: TextAlign.center,
textDirection: TextDirection.ltr,
);
tp.text = TextSpan(
text: title.toStringAsFixed(0),
style: TextStyle(
fontSize: 35.0,
color: Theme.of(context).accentColor,
letterSpacing: 1.0,
fontFamily: 'Roboto Bold',
),
);

PictureRecorder recorder = new PictureRecorder();
Canvas c = new Canvas(recorder);

tp.layout();
tp.paint(c, new Offset(20.0, 10.0));

/* Do your painting of the custom icon here, including drawing text, shapes, etc. */

Picture p = recorder.endRecording();
ByteData pngBytes =
await (await p.toImage(tp.width.toInt() + 40, tp.height.toInt() + 20))
.toByteData(format: ImageByteFormat.png);

Uint8List data = Uint8List.view(pngBytes.buffer);

return BitmapDescriptor.fromBytes(data);
}

使用方法:

BitmapDescriptor bitmapDescriptor = await createCustomMarkerBitmap(...);

Marker marker = Marker(
/* in addition to your other properties: */
icon: bitmapDescriptor
);

关于flutter - 如何使用 google_maps_flutter 在标记内创建文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58172727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com